S
S
shanik2018-04-04 15:07:11
PHP
shanik, 2018-04-04 15:07:11

How to sort such an array?

there is an array:

Array
(
    [Pinaceae Lindl.] => Array
        (
            [aborigen_count_rod] => Array
                (
                    [0] => 1
                )

            [aborigen_count_vid] => Array
                (
                    [0] => 2
                )

        )

    [Asteraceae Dumort.] => Array
        (
            [aborigen_count_rod] => Array
                (
                    [0] => 1
                )

            [aborigen_count_vid] => Array
                (
                    [0] => 1
                )

        )

    [Adoxaceae Trautv.] => Array
        (
            [aborigen_count_rod] => Array
                (
                    [0] => 1
                )

            [aborigen_count_vid] => Array
                (
                    [0] => 3
                )

        )

    [Magnoliopsida] => Array
        (
            [aborigen_count_rod] => Array
                (
                    [0] => 1
                )

            [aborigen_count_vid] => Array
                (
                    [0] => 1
                )

        )

)

How to sort by [aborigen_count_vid] in descending order without breaking the parent?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shanik, 2018-04-07
@shanik

uasort($count_data, function($a, $b) {
  if ($a['aborigen_count_vid'][0] === $b['aborigen_count_vid'][0]) return 0;
  return $a['aborigen_count_vid'][0] < $b['aborigen_count_vid'][0] ? 1 : -1;
});

M
Maxim Fedorov, 2018-04-04
@qonand

use standard uasort

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question