【问题标题】:Get lowest key - array value in multidimensional array PHP获取最低键 - 多维数组PHP中的数组值
【发布时间】:2021-05-15 17:18:30
【问题描述】:

所以,我得到的数组看起来像这样:

[65]=>
  array(2) {
    [0]=>
    array(2) {
      ["p"]=>
      float(234)
      ["sp"]=>
      float(234)
    }
    [1]=>
    array(2) {
      ["p"]=>
      float(53)
      ["sp"]=>
      float(5)
    }
    [2]...
    [3]...

  }

这个想法是遍历key 65数组的0-N个值中的每一个,只保留一个具有最小“p”的值,其他的应该被删除/过滤掉。

这应该在 PHP 中完成。 有人知道吗?

我尝试过这样的事情:

$array = array_filter($array, function ($value, $key) use ($a) {
   return $a['p'] <= $value['p'];
}, ARRAY_FILTER_USE_BOTH);

其中 $value 是 65 个键控数组中的 1 个元素,$a 是当前动态添加的对。因此,当它添加时,我会检查现有元素,如果它最低,它应该保留,而其他元素会立即被过滤掉,但如果它更高,它应该自动被过滤掉。

谢谢!

【问题讨论】:

    标签: php arrays multidimensional-array key


    【解决方案1】:

    您可以使用array_reduce() 获得最低的“p”值:

    $arr = [
        65 => [
            ["p" => 234, "sp" => 234],
            ["p" => 53, "sp" => 5],
            ["p" => 530, "sp" => 5],
        ]
    ];
    
    function getLowestKey($carry, $item) {
        if ($item['p'] < $carry || !$carry) {
            $carry = $item['p'];
        }
        return $carry;
    }
    
    $lowestKey = array_reduce($arr[65], 'getLowestKey');
    var_dump($lowestKey); // int(53)
    

    编辑:

    我刚刚注意到您的问题还有第二部分,对此感到抱歉。一旦找到“最低 p”,您就可以使用该知识过滤数组:

    $lowestPs = array_filter($arr[65], function($item) use ($lowestKey) {
        return $item['p'] == $lowestKey;
    });
    
    var_dump($lowestPs);
    /*
    array(2) {
      [1]=>
      array(2) {
        ["p"]=>
        int(53)
        ["sp"]=>
        int(5)
      }
      [2]=>
      array(2) {
        ["p"]=>
        int(53)
        ["sp"]=>
        int(5)
      }
    }
    */
    

    即使多个条目具有相同的最低“p”值(如上例中的 53),此解决方案仍然有效。

    【讨论】:

    • 谢谢伙计。不知道如何,但它完全按照它应该的方式工作。 :)
    【解决方案2】:

    使用array_column() 对键65 内记录的'p' 值执行array_multisort()

    <?php
    $col = 'p'; // set the column you want to order on
    $column = array_column($arr[65], $col);
    array_multisort($column, SORT_ASC, $arr[65]);
    $arr[65] = $arr[65][0]; // only keep the record with lowest 'p' value
    

    demo

    【讨论】:

    • 稍微修改了代码,因此您不必对整个$arr[65] 进行排序。 Code。 - 如果你只想保留一个值,它应该是$arr[65] = [&lt;elementYouWantToKeep](注意括号,所以它仍然是一个数组)
    • @berend 我试过了,但我得到了 array_multisort(): Array size are compatible 错误。在 array_multisort($column, SORT_ASC, $sortedArray[$id]);
    【解决方案3】:

    如果嵌套层数超过 1 个,您还可以使用递归方法检查 p 的值,使数组保持最低值。

    $arrays = [
        65 => [
            ["p" => 234, "sp" => 234],
            [
                ["p" => 53,"sp" => 5],
                [
                    ["p" => 54,"sp" => 1],
                    ["p" => 53,"sp" => 7],
                ]
            ], [
                "p" => 255,
                "sp" => 235
            ],
        ]
    ];
    
    function loop($array, &$coll = [], &$min = null)
    {
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                loop($value, $coll, $min);
            } elseif ($key === "p") {
                if ($min === null) $min = $value;
                if ($min > $value) {
                    $coll = [$array];
                    $min = $value;
                    continue;
                }
                if($value === $min) $coll[] = $array;
            }
        }
        return $coll;
    }
    
    $arrays[65] = loop($arrays[65]);
    var_dump($arrays);
    

    输出

    array(1) {
      [65]=>
      array(2) {
        [0]=>
        array(2) {
          ["p"]=>
          int(53)
          ["sp"]=>
          int(5)
        }
        [1]=>
        array(2) {
          ["p"]=>
          int(53)
          ["sp"]=>
          int(7)
        }
      }
    }
    

    查看另一个php demo

    【讨论】:

      猜你喜欢
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多