E
E
EvgenyJozef30002019-05-31 15:02:52
PHP
EvgenyJozef3000, 2019-05-31 15:02:52

How to sort an array using usort() in PHP?

Good evening.
There is an array:

[0] => Array
        (
            [code] => CAS
            [name] => Корпуса
            [code_parent] => 171
        )

    [1] => Array
        (
            [code] => MIN
            [name] => Майнинг
            [code_parent] => 171
        )

    [2] => Array
        (
            [code] => DVD
            [name] => Оптические приводы
            [code_parent] => 171
        )

    [3] => Array
        (
            [code] => VGP
            [name] => Видеокарты
            [code_parent] => 171
        )

    [4] => Array
        (
            [code] => BAC
            [name] => Фен-Щётки
            [code_parent] => 1434
        )

    [5] => Array
        (
            [code] => EPI
            [name] => Эпиляторы
            [code_parent] => 1434
        )

    [6] => Array
        (
            [code] => HCL
            [name] => Машинки для стрижки
            [code_parent] => 1434
        )

    [7] => Array
        (
            [code] => HDR
            [name] => Фены
            [code_parent] => 1434
        )

    [8] => Array
        (
            [code] => BSC
            [name] => Напольные весы
            [code_parent] => 1434
        )

    [9] => Array
        (
            [code] => SHR
            [name] => Бритвы
            [code_parent] => 1434
        )

    [10] => Array
        (
            [code] => 1434
            [name] => Красота и здоровье
        )

    [11] => Array
        (
            [code] => 171
            [name] => Комплектующие
        )

How to sort an array with the usort() function so that the following array is obtained:
[0] => Array
        (
            [code] => 1434
            [name] => Красота и здоровье
        )
    [1] => Array
        (
            [code] => BAC
            [name] => Фен-Щётки
            [code_parent] => 1434
        )

    [2] => Array
        (
            [code] => EPI
            [name] => Эпиляторы
            [code_parent] => 1434
        )

    [3] => Array
        (
            [code] => HCL
            [name] => Машинки для стрижки
            [code_parent] => 1434
        )

    [4] => Array
        (
            [code] => HDR
            [name] => Фены
            [code_parent] => 1434
        )

    [5] => Array
        (
            [code] => BSC
            [name] => Напольные весы
            [code_parent] => 1434
        )

    [6] => Array
        (
            [code] => SHR
            [name] => Бритвы
            [code_parent] => 1434
        )
    [7] => Array
        (
            [code] => 171
            [name] => Комплектующие
        )
    [8] => Array
        (
            [code] => CAS
            [name] => Корпуса
            [code_parent] => 171
        )

    [9] => Array
        (
            [code] => MIN
            [name] => Майнинг
            [code_parent] => 171
        )

    [10] => Array
        (
            [code] => DVD
            [name] => Оптические приводы
            [code_parent] => 171
        )

    [11] => Array
        (
            [code] => VGP
            [name] => Видеокарты
            [code_parent] => 171
        )

Sort by key [code_parent].

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AN, 2019-05-31
@EvgenyJozef3000

This issue can also be considered closed. He got the solution here .

A
Alexander Aksentiev, 2019-05-31
@Sanasol

You can’t sort it like that, you need to group it into hierarchical subarrays, if more than one level, then generally using recursion.
You do not have sorting, but it is not clear what happens randomly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question