PHP Search Array column for match

php search multidimensional array
php search multidimensional array for value
php search array of objects
php array search key
php array search callback
php search multidimensional array for multiple values
php get value from associative array by key
php get array value by key

I have an array as below, which has multiple columns. I want to search in the first column for a specific value, and have the rows that match returned. Is that possible to do?

For example:

Array (
[0] => Array ( [id] => 1 [column2] => value2 [column3] => value3 [column4] => value4 [column5] => value5 ) 
[1] => Array ( [id] => 1 [column2] => value2 [column3] => value3 [column4] => value4 [column5] => value5 ) 
[2] => Array ( [id] => 2 [column2] => value2 [column3] => value3 [column4] => value4 [column5] => value5 
)

So let's say I want to search the "id" column for "1" and have the results displayed. How can this be done? Thank you so much!

Since you have an nested Array you need two iterations:

$filtered = array();
$rows = Your Array;
foreach($rows as $index => $columns) {
    foreach($columns as $key => $value) {
        if ($key == 'id' && $value == '1') {
            $filtered[] = $columns;
        }
    }
}

This should do the job.

array_search - Manual, To return the keys for all matching values, use array_keys() with the optional search_value search an array like another or not array_match which can match array_search (PHP 4 >= 4.0.5, PHP 5, PHP 7) array_search — Searches the array for a given value and returns the first corresponding key if successful

If you are using PHP >= 5.5, then you can use the new array_column(), in conjunction with array_keys() and array_map().

Given your array, $array:

$keys = array_keys(array_column($array, 'id'), 1);
$new_array = array_map(function($k) use ($array){return $array[$k];}, $keys);

See demo

array_column - Manual, array_column — Return the values from a single column in the input array The column to use as the index/keys for the returned array. This value may be the Because we call back to this function, check if call was made by self to // prevent  If the value is found in the array more than once, the first matching key is returned. PHP Version: 4.0.5+ PHP Changelog: This function returns NULL if invalid parameters are passed to it (this applies to all PHP functions as of 5.3.0). As of PHP 4.2.0, this function returns FALSE on failure instead of NULL.

Use this function :

global $result;
function array_searc_result($array,$key,$value)
{
    global $result;
    foreach($array as $k=>$v)
    {
        if(array_key_exists($key,$v) && ($v[$key] == $value))
        {
            $result[] = $v;
        }
    }
    return $result;;
}
$data = array_searc_result($array,'id',2);
echo '<pre>';
print_r($data);
echo '</pre>';

$array is your given array variable.

PHP multidimensional array search by value, Multidimensional array search using array_search() method:​​ The array_search() is an inbuilt function which searches for a given value related to the given array column/key. This function only returns the key index instead of a search path. using array_search for multi dimensional array. The array_search('needle', array_column Deleting an element from an array in PHP.

I found a much simpler solution that I think is worthwhile sharing with the world

in_array(1, array_column($yourArray, 'id'));

Tested on PHP >= 5.5

PHP array_column() Function, The array_column() function returns the values from a single column in the input array. Syntax. array_column(array, column_key, index_key). Parameter Values  It generates a 500k-member array of arrays and searches through it for a value in the last member. I compared a function like the accepted answer, to the two array_column one-liner answers. I modified them all to return the actual discovered array, not just the key, because usually that's my use case.

I use this helper to find matches by key / value:

function array_search_by_key($array, $key, $value) {
    if(!is_array($array)) {
        return [];
    }
    $results = [];
    foreach($array as $element) {
        if(isset($element[$key]) && $element[$key] == $value) {
            $results[] = $element;
        }
    }
    return $results;
}

PHP array_search() Function, If the value is found in the array more than once, the first matching key is returned​. PHP Version: 4.0.5+. PHP Changelog: This function returns NULL if invalid  This function searches the values in an array for a match to the search term, and returns the corresponding key if found. If more than one match exists, the key of the first matching value is

array_find(), gistfile1.php. <?php. /**. * Case in-sensitive array_search() with partial matches check for multidimentional array foreach (array_column($haystack, $column) as is found more than once and if some return an array of the keys matching. array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.

PHP: array_key_exists() function, PHP: Checks if the given key or index exists in an array. The array_key_exists() function is used to check whether a specified key is present in  If you search for numbers, in_array will convert any strings in your array to numbers, dropping any letters/characters, forcing a numbers-to-numbers comparison. So if you search for 1234, it will say that '1234abcd' is a match.

How to Check If a Key Exists in an Array in PHP, How to compare two array values in PHP · Previous Page Next Page. Advertisements. Bootstrap UI Design Templates  Definition and Usage. The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.

Comments
  • Bonus question: How would I be able to know how many results were found (so how many rows were returned in the array)? Is that possible to do? Thank you!
  • To get the number of items in an Array use count() function. Like $num_results = count($filtered).
  • Thanks Mark, this worked exactly the way I wanted to. Cheers!
  • Works just fine without the use of Globals. Also, be aware of the quasi-typo in the function name.
  • This checks the existence of, but not the location of the rows containing the needle. This doesn't provide the output detailed in the question.