我有一个数组:
Array ( [0] => Array ( [id] => 10 [name] => Michael [score] => 40 ) [1] => Array ( [id] => 9 [name] => David [score] => 30 ) [2] => Array ( [id] => 8 [name] => David [score] => 20 ) )
我想找到一个元素的id。例如:如果我输入name = Michael并且得分= 40,那么它将显示id为0,依此类推。
答案 0 :(得分:0)
您可以使用按名称和分数搜索的foreach循环。下面的代码搜索得分为40的迈克尔,并使用找到的ID填充变量$id
:
$id = null;
foreach($yourArray as $key => $arr) {
if($arr['name'] == 'Michael' and $arr['score'] == 40) {
$id = $key;
break;
}
}