2017-06-23 84 views
1

我不能为我的生活弄清楚如何通过多维数组搜索key =>值对,然后A)返回它所属的数组或者不同的特定键=>存在于同一个数组中的值对。搜索多维数组并返回该数组中的特定值

我的数组是:

$pages = array(
    array(
     'pageId' => 10, 
     'title' => 'Welcome', 
     'theme' => 'basic' 
    ), 
    array(
     'pageId' => 11, 
     'title' => 'Home', 
     'theme' => 'basic' 
    ), 
    array(
     'pageId' => 12, 
     'title' => 'Login', 
     'theme' => 'basic' 
    ) 
); 

我已经试过

$theme = array_search(10, array_column($search, 'pageId')); 

但它一直返回一个int,而不是价值basic,因为我想。

我想要的只是值或数组与键=>值对或它所属的整个数组。

+0

你在说这个''主题'=>'basic''吗?你想要'基本'这是主题的价值吗? –

回答

3

试试这个最简单的,希望这会有所帮助。这里我们使用array_column

Try this code snippet here

$result=array_column($pages,"theme" ,'pageId'); 
if(isset($result[$toSearch])) 
{ 
    echo $result[$toSearch]; 
} 
+1

这完全是我在找的东西。谢谢! –

0

您可以使用array_filter,live demo

array_filter($array, function($v) use($searchKey, $searchValue) { 
    return $v[$searchKey] == $searchValue; 
});