PHP Search Array column for match

24,876

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

These steps will always return a row in an array that uses a unique id column (in this example in the first column, 0)

1) Get an array of just IDs

$ids = array_column($my_table, 0);

2) Find the row with my ID

$row_index = array_search($id, $ids); (where $id is a certain ID)

3) Then I could use

$my_table[$row_index][n] (where n is a given column)

Share:
24,876
David
Author by

David

Updated on March 30, 2020

Comments

  • David
    David about 4 years

    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!

  • David
    David almost 10 years
    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!
  • Mario Werner
    Mario Werner almost 10 years
    To get the number of items in an Array use count() function. Like $num_results = count($filtered).
  • MichelV69
    MichelV69 about 6 years
    Works just fine without the use of Globals. Also, be aware of the quasi-typo in the function name.
  • mickmackusa
    mickmackusa about 5 years
    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.