Z
Z
Zubastik_12018-03-15 21:53:31
PHP
Zubastik_1, 2018-03-15 21:53:31

How to convert an array to an array with one field maximum in value (with example)?

How can I convert such an array to the one below, that is, leave only the maximum values ​​for one field?

Array
(
    [Dima] => Array
        (
            [0] => Array
                (
                    [id] => 61
                    [price] => 9
                    [time] => 2018-03-13 18:31:01
                )

            [1] => Array
                (
                    [id] => 62
                    [price] => 6
                    [time] => 2018-03-13 18:31:01
                )

            [2] => Array
                (
                    [id] => 63
                    [price] => 32
                    [time] => 2018-03-13 18:31:01
                )

        )

    [Nick] => Array
        (
            [0] => Array
                (
                    [id] => 65
                    [price] => 68
                    [time] => 2018-03-13 18:32:01
                )

            [1] => Array
                (
                    [id] => 67
                    [price] => 91
                    [time] => 2018-03-14 00:00:02
                )

        )

)

upstream convert to this bottom?
Array
(
    [Dima] => Array ([price] => 32) //максимальное значение прайса у Димы
    [Nick] => Array ([price] => 91)//максимальное значение прайса у Ника

)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nick Sdk, 2018-03-16
@Zubastik_1

specifically your case

$new_arr = [];
foreach ($arr as $name => $item)
    $new_arr[$name] = ['price' => max(array_column(array_values($item), 'price'))];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question