9

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!

0

7 Answers 7

17

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

Sign up to request clarification or add additional context in comments.

Comments

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.

2 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).
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)

Comments

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.

1 Comment

Works just fine without the use of Globals. Also, be aware of the quasi-typo in the function name.
1

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

1 Comment

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.
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; } 

Comments

0

Here's a one liner, that allows for wild card searches:

$arr=[ 0=>['id'=>1,'column2'=>'value2','column3'=>'value3','column4'=>'value4','column5'=>'value5'], 1=>['id'=>1,'column2'=>'value5','column3'=>'value3','column4'=>'value4','column5'=>'value5'], 2=>['id'=>2,'column2'=>'value2','column3'=>'value3','column4'=>'value4','column5'=>'value5'], ]; $out=preg_grep("/^1$/", array_column($arr,'id')); // the one liner var_dump($out); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.