所以我有以下示例。
$array = [
[
"id" => "1",
"color" => "green"
],
[
"id" => "2",
"color" => "red"
],
[
"id" => "3",
"color" => "blue"
]
];
因此,我想遍历具有数组的数组,如果我匹配颜色“ red”以在名为$ match_id的新变量中提取“ id” 2。谢谢您的帮助!
答案 0 :(得分:4)
您可以尝试这样的事情
foreach ( $array as $item ) {
if ( $item['color'] == 'red' ) {
$match_id = $item['id'];
break;
}
}