8

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
  • 316
  • 1
  • 2
  • 10

6 Answers6

15

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

Mark Miller
  • 7,442
  • 2
  • 16
  • 22
5

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.

Mario Werner
  • 1,771
  • 14
  • 24
  • 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! – David Jun 28 '14 at 20:34
  • 1
    To get the number of items in an `Array` use `count()` function. Like `$num_results = count($filtered)`. – Mario Werner Jun 29 '14 at 10:43
2

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

MontrealDevOne
  • 1,034
  • 3
  • 17
  • 30
  • 4
    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. – mickmackusa Mar 24 '19 at 23:21
2

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)

Chris
  • 21
  • 3
1

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.

Suman Biswas
  • 853
  • 10
  • 19
0

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;
}
Tool
  • 12,126
  • 15
  • 70
  • 120