更新时间:2023-11-28 16:33:22
您可以使用略有不同的循环.
You can use a slightly different loop.
foreach ($var as $item) {
if ($item['id'] == 2) {
$newvar = $item;
break;
}
}
您还可以使用array_filter
You could also use array_filter
$id = 2;
$newvar = array_filter($var, function($x) use ($id) { return $x['id'] == $id; });
但是它很可能效率较低,因为它必须检查数组的每个元素.
but it would most likely be less efficient as it would have to check every element of the array.