我尝试从多维数组内的最大值获取密钥。
这是我的样本数组
$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'))
);
}
自从我受到启发
但是结果总是一个空数组。我猜想当我像尝试使用associativ数组时array_keys不起作用。
还有其他方法可以做到这一点吗?当然不用我自己收集价值。
根据您的回应,我建议您进行以下小调整:
<?php
$resultCache[129] = [
'total' => 1000,
'free_from' => "2000",
'addinupshippingcosts' => "0",
'articles' => [
['shipping_costs_total' => 11],
['shipping_costs_total' => 23],
['shipping_costs_total' => 25], //<= i want the key of this array entry
['shipping_costs_total' => 12],
]
];
foreach($resultCache as $r) {
$col = array_column($r['articles'], 'shipping_costs_total');
$highest = array_keys($col, max($col));
$highest = $highest[0] ?: false;
echo $highest;
}
希望这可以帮助,
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句