0

I have an array where I need to find the max value. I have looked at many examples and logged questions here but I can not seem to find an answer to my specific issue.

Array
(
    [0] => Array
        (
            [xaxis] => Test Test Test
            [yaxis] => 19875.00
        )

    [1] => Array
        (
            [xaxis] => Test1
            [yaxis] => 10375.00
        )

    [2] => Array
        (
            [xaxis] => Test2
            [yaxis] => 76000.00
        )

    [3] => Array
        (
            [xaxis] => test3
            [yaxis] => 4451.61
        )

    [4] => Array
        (
            [xaxis] => test4
            [yaxis] => 6225.81
        )

    [5] => Array
        (
            [xaxis] => test5
            [yaxis] => 3000.00
        )

    [6] => Array
        (
            [xaxis] => test6
            [yaxis] => 2000.00
        )

)

I have tried the following:

echo max($data[0]['yaxis']);

This will give an error obviously

echo max($data[0]);

This returns 'Test Test Test' instead of the correct '76000'

echo max($data);

This returns 'array'

user389391
  • 97
  • 1
  • 8

1 Answers1

0

max() works for simple arrays. Turn yours into one.

$values = array_map(function ($el) {
    return $el['yaxis'];
}, $data)

echo max($values);

You can make it into a one liner too.

echo max(array_map(function ($el) { return $el['yaxis']; }, $data));
echo max(array_map(fn($el) => $el['yaxis'], $data));
IGP
  • 14,160
  • 4
  • 26
  • 43