3 回答
TA贡献1862条经验 获得超6个赞
您可以array_column
使用array_keys
和max
$amountArr = array_column($arr, null, 'amount'); print_r($amountArr[max(array_keys($amountArr))]);
工作示例:- https://3v4l.org/d2WRf
TA贡献1829条经验 获得超4个赞
Rakesh 的解决方案通过在调用期间使用amount
作为新键消除了多个限定子数组的可能性。array_column()
至少有几种不同的方法可以执行此任务。这只是一个 - 提取最大值,然后使用该值过滤整个数组。
代码:(演示)
$array = [
[
'code' => 'Custom discount',
'amount' => 3514.55,
'type' => 'fixed_amount'
],
[
'code' => 'MerOrder300',
'amount' => 400.00,
'type' => 'fixed_amount'
],
[
'code' => 'MerOrder400',
'amount' => 3514.55,
'type' => 'fixed_amount'
],
[
'code' => 'MerOrder450',
'amount' => 450.00,
'type' => 'fixed_amount'
]
];
$max = max(
array_column($array, 'amount')
);
var_export(
array_filter(
$array,
function($subarray) use ($max) {
return $subarray['amount'] == $max;
}
)
);
TA贡献1895条经验 获得超7个赞
您可以使用foreach:
$arr = [
[
'code' => 'Custom discount',
'amount' => 3514.55,
'type' => 'fixed_amount'
],
[
'code' => 'MerOrder300',
'amount' => 400.00,
'type' => 'fixed_amount'
],
[
'code' => 'MerOrder400',
'amount' => 400.00,
'type' => 'fixed_amount'
],
[
'code' => 'MerOrder450',
'amount' => 450.00,
'type' => 'fixed_amount'
]
];
$max = $arr[0];
foreach ($arr as $line) {
if ($line['amount'] > $max['amount']) {
$max = $line;
}
}
print_r($max);
- 3 回答
- 0 关注
- 87 浏览
添加回答
举报