我有一个数组,在这个数组中我有列price,现在我想做一个价格等于 5 以及超过 5。我知道使用 foreach 循环我们可以实现这一点,任何其他方式都可以实现我的预期答案?
$mainArray = [
"key1" => ["name" => "A", "price" => 5],
"key2" => ["name" => "B", "price" => 7],
"key3" => ["name" => "C", "price" => 2],
"key4" => ["name" => "D", "price" => 9]
];
我的代码
<?php
$mainArray = [
"key1" => ["name" => "A", "price" => 5],
"key2" => ["name" => "B", "price" => 7],
"key3" => ["name" => "C", "price" => 2],
"key4" => ["name" => "D", "price" => 9]
];
$maxPrice = 5;
$mainArray = array_filter($mainArray, function($value) {
return $value['price'] >= $maxPrice;
});
print_r($mainArray);
?>
但我收到错误 E_NOTICE : type 8 -- Undefined variable: maxPrice -- at line 11
慕虎7371278
胡子哥哥