Search code examples
phparraysmultidimensional-arraykey

Get lowest key - array value in multidimensional array PHP


So, I got array that looks something like this:

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

  }

The idea is to go through each of 0 - N values of key 65 array, and only keep one with smallest "p", others should be removed / filtered out.

This should be done in PHP. Anyone has any idea?

I tried something like this:

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

where $value is 1 of elements inside 65 keyed-array and $a is current pair that is being added dynamically. So when ever its added, I go through existing elements and if its lowest, it should stay, and others get instant filtered out, but if its higher, it should automatically be filtered out.

Thank you!


Solution

  • You can use array_reduce() to get the lowest "p"-value:

    $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)
    

    Edit:

    I just noticed there is a second part to your question, sorry about that. Once you found out the "lowest p" you can then just filter the array with that knowledge:

    $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)
      }
    }
    */
    

    This solution works even if multiple entries have the same lowest "p" value (like 53 in the above example), all of those will stay.