Issue
What's the best/fastest way to get the keys of an array by a search of a value in the 2nd level arrays?
$test = array(
'name_01' => array('yellow', 'red', 'blue', 'black', 'white', 'purple'),
'name_02' => array('red', 'blue', 'white', 'green'),
'name_03' => array('blue', 'pink', 'purple', 'blue'),
'name_04' => array('white', 'black', 'red'),
'name_05' => array('yellow', 'white', 'pink', 'black')
);
For example the search by pink should return array('name_03', 'name_05')
Solution
A simple foreach()
with in_array()
is enough
$search = 'pink';
foreach($test as $key=>$arr){
if(in_array($search,$arr)){
echo $key.PHP_EOL;
}
}
Output : https://3v4l.org/HVem8
If you want array as an output : https://3v4l.org/8e0sj
Answered By - Anant Kumar Singh
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.