Reputation: 597
I cannot for the life of me figure out how to search through a multidimensional array for a key => value pair, and then either A) Return the array it belongs to or a different specific key => value pair that exists in the same array.
My array is:
$pages = array(
array(
'pageId' => 10,
'title' => 'Welcome',
'theme' => 'basic'
),
array(
'pageId' => 11,
'title' => 'Home',
'theme' => 'basic'
),
array(
'pageId' => 12,
'title' => 'Login',
'theme' => 'basic'
)
);
I've tried
$theme = array_search(10, array_column($search, 'pageId'));
but it keeps returning an int and not the value basic
as I am wanting.
I would like either just the value or an array with the key => value pair or the whole array it belongs to.
Upvotes: 1
Views: 89
Reputation: 28529
You can use array_filter, live demo.
array_filter($array, function($v) use($searchKey, $searchValue) {
return $v[$searchKey] == $searchValue;
});
Upvotes: 0
Reputation: 15141
Try this simplest one, hope this will be helpful. Here we are using array_column
$result=array_column($pages,"theme" ,'pageId');
if(isset($result[$toSearch]))
{
echo $result[$toSearch];
}
Upvotes: 3