【问题标题】:Retrieve key from max value in multidimensional associativ array从多维关联数组中的最大值检索键
【发布时间】: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 arrayFind highest value in multidimensional array

但结果总是一个空数组。我想当我尝试使用关联数组时,array_keys 不起作用。

还有其他方法可以实现吗?当然不是我自己收集的值。

【问题讨论】:

  • 你期望的输出是什么? 25
  • 我希望在这种情况下为 0,因为这是具有最高值 (25) 的文章数组的键

标签: php arrays associative-array


【解决方案1】:

这个怎么样?

$max = array_column($resultCache[129]['articles'], 'shipping_costs_total');
arsort($max);
reset($max);
$first_key = key($max);
echo $first_key;

如果你已经 (PHP 7 >= 7.3.0) 那么

$max = array_column($resultCache[129]['articles'], 'shipping_costs_total');
arsort($max);
reset($max);
echo array_key_first($max);

工作演示: https://3v4l.org/t472P

【讨论】:

    【解决方案2】:

    根据您的回复,我建议进行以下小调整:

    <?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;
    }
    

    希望这会有所帮助,

    【讨论】:

    • 我需要密钥 ^^ 而不仅仅是最高值
    • @Dwza,我建议在这种情况下使用foreach 循环(因为array_reduce 本身并不为我们提供当前索引)
    • 当然我有一个用于 resultCache 的 foreach 但我想拒绝为文章数组提供一个 foreach :D 并且猜这看起来比我的更好^^
    【解决方案3】:

    我终于想通了......对于任何可能需要这个的人......

    $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(
            array_column($r['articles'], 'shipping_costs_total'),
            max(array_column($r['articles'], 'shipping_costs_total'))
        );
        echo $highest[0]
    }
    

    显然传递的数组也需要array_column。

    【讨论】:

    • 在这里查看我的答案stackoverflow.com/a/59107293/1138192 和工作演示。您可以根据需要修改/添加 foreach
    • 我只是看了看,但这看起来很......“不干净”,如果你知道我的意思的话:) 排序、重置、获取密钥比...... :D 非常感谢,而且投票错误因为它有效。但我想我会选择我找到的解决方案。
    • 完全同意你的观点,但我想它比 foreach, array_keys,array_column,max,array_column.... 你觉得呢?
    • 我不知道,使用 array_colum 或 array_keys 最终会得到期望值...我不知道是否有可能提供错误的数据并最终导致一些意外。我也不知道什么会花费更多的性能。正如您所看到的,由于 $resultCache 有很多项目,因此无论如何都必须通过 foreach。但你的确实更短!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-16
    相关资源
    最近更新 更多