【发布时间】:2019-11-29 14:54:28
【问题描述】:
我尝试从多维数组中的最大值获取键。
这是我的示例数组
$resultCache[129] = [
'total' => 1000,
'free_from' => "2000",
'addinupshippingcosts' => "0",
'articles' => [
['shipping_costs_total' => 25], //<= i want the key of this array entry
['shipping_costs_total' => 12],
]
];
首先我想我可以选择一些像
foreach($resultCache as $s => $r){
$highest = array_keys(
$resultCache[$s]['articles'],
max(array_column($resultCache[$s]['articles'], 'shipping_costs_total'))
);
}
因为我受到了启发
Return index of highest value in an array 和 Find highest value in multidimensional array。
但结果总是一个空数组。我想当我尝试使用关联数组时,array_keys 不起作用。
还有其他方法可以实现吗?当然不是我自己收集的值。
【问题讨论】:
-
你期望的输出是什么? 25
-
我希望在这种情况下为 0,因为这是具有最高值 (25) 的文章数组的键
标签: php arrays associative-array