我有一个类似于下面的数组。我需要找到一种通过使用键/值对来提取值的方法,例如使用'parent_id' => 3
。我希望得到array ( id = 2, label = Content Creation, link_url = '', parent_id = 3 )
的所有值。
我尝试使用array_intersect()
但没有成功。
感谢您的帮助。
Array (
[0] => Array ( [id] => 1 [label] => Web Development [link_url] => [parent_id] => 1 )
[1] => Array ( [id] => 2 [label] => Content Creation [link_url] => [parent_id] => 3 )
[2] => Array ( [id] => 3 [label] => PHP Jobs [link_url] => /simple_link.php [parent_id] => 1 )
[3] => Array ( [id] => 4 [label] => OSCommerce projects [link_url] => /another_link.php [parent_id] => 4 )
)
答案 0 :(得分:2)
我认为你可以在数组中循环,并将你想要的parent_id
与if条件相匹配
foreach($array as $data)
{
if($data['parent_id'] == '3')
{
echo $data['id'] . ' ' . $data['label'] . ' ' . $data['link_url'];
}
}
答案 1 :(得分:0)
你也可以使用array_filter来解决这些问题
$matching_results = array_filter($data, function($item) {
return ($item['parent_id'] == 3);
});
它将遍历数据,$ matching_results将是每个$ item的数组,返回true