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'

2
  • foreach + compare last highest value with the current one. Commented Dec 14, 2021 at 20:45
  • 1
    $yaxisMax = max(array_column($arr, 'yaxis')); Commented Dec 14, 2021 at 20:47

1 Answer 1

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));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.