1

I try to get the key from the largest value inside an multidimensional array.

Here is my sample array

$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],
    ]
];

First I thought I could go for some like

foreach($resultCache as $s => $r){
    $highest = array_keys(
        $resultCache[$s]['articles'],
        max(array_column($resultCache[$s]['articles'], 'shipping_costs_total'))
    );
}

since I got inspired by

Return index of highest value in an array and Find highest value in multidimensional array.

But the result is always an empty array. I guess array_keys dont work when I use an associativ array like I tried.

Is there any other way to achive this? Of course without collecting the values by my self.

Dwza
  • 6,494
  • 6
  • 41
  • 73

3 Answers3

2

How about this?

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

If you've (PHP 7 >= 7.3.0) then

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

WORKING DEMO: https://3v4l.org/t472P

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
1

Working off of your response, I would suggest these small tweaks:

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

Hope this helps,

Miroslav Glamuzina
  • 4,472
  • 2
  • 19
  • 33
  • i need the key ^^ not simply the highest value – Dwza Nov 29 '19 at 15:03
  • @Dwza, I would recommend using a `foreach` loop in this case (as `array_reduce` doesn't natively provide us with the current index) – Miroslav Glamuzina Nov 29 '19 at 15:14
  • of course i have an foreach for resultCache but i want to deny an foreach for the articles array :D And guess this looks even more better than mine ^^ – Dwza Nov 29 '19 at 15:33
1

I finaly figured out.... for anyone who may need this...

$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]
}

obviously the passing array needs the array_column as well.

Dwza
  • 6,494
  • 6
  • 41
  • 73
  • Have a look on my answer here https://stackoverflow.com/a/59107293/1138192 and working demo. You can modify/add foreach if you wish as per your requirement – A l w a y s S u n n y Nov 29 '19 at 15:15
  • I just looked but this looks so... "unclean" if you know what I mean :) sorting, resetting, getting key than.... :D thank you very much, and ill vote it because it works. But I guess I go with my found solution. – Dwza Nov 29 '19 at 15:18
  • totally agreed with you, but I guess it is better than foreach, array_keys,array_column,max,array_column.... what do you think ? – A l w a y s S u n n y Nov 29 '19 at 15:23
  • I dont know, using array_colum or array_keys will end up in expectet values... I dont know if its possible to give wrong data and end up with some unexpected. Also I dont know what costs more performance. And as you can see, ill have to go through an foreach anyway since $resultCache has a lot of items. But yours is truely shorter! – Dwza Nov 29 '19 at 15:26