PHP перемещает массив дерева подкатегорий категорий в пользовательский 2D массив

У меня есть родительский дочерний массив, похожий на этот:

массив
(
[0] => Массив
(
[category_id] => 1
[category_name] => Домой  Сад
[parent_id] => 0
[уровень] => 1
)

[1] => Массив
(
[category_id] => 2
[category_name] => Кухня  обеденный
[parent_id] => 1
[уровень] => 2
)

[2] => Массив
(
[category_id] => 3
[category_name] => Еда  Перевозчики напитков
[parent_id] => 2
[уровень] => 3
)

[3] => Массив
(
[category_id] => 4
[category_name] => Колбы
[parent_id] => 3
[уровень] => 4
)

[4] => Массив
(
[category_id] => 5
[category_name] => Одежда  аксессуары
[parent_id] => 0
[уровень] => 1
)

[5] => Массив
(
[category_id] => 6
[category_name] => Одежда
[parent_id] => 5
[уровень] => 2
)

[6] => Массив
(
[category_id] => 7
[category_name] => Шорты
[parent_id] => 6
[уровень] => 3
)

[7] => Массив
(
[category_id] => 8
[category_name] => Рубашки  верхушки
[parent_id] => 6
[уровень] => 3
)

[8] => Массив
(
[category_id] => 9
[category_name] => Перемычки  Cardigans
[parent_id] => 8
[уровень] => 4
)

[9] => Массив
(
[category_id] => 10
[category_name] => Футболки
[parent_id] => 8
[уровень] => 4
)

[10] => Массив
(
[category_id] => 11
[category_name] => Юбки
[parent_id] => 6
[уровень] => 3
)

[11] => Массив
(
[category_id] => 12
[category_name] => Рубашки  Блузы
[parent_id] => 8
[уровень] => 4
)

[12] => Массив
(
[category_id] => 13
[category_name] => Брюки  Джинсы
[parent_id] => 6
[уровень] => 3
)

[13] => Массив
(
[category_id] => 14
[category_name] => Чинос
[parent_id] => 13
[уровень] => 4
)

[14] => Массив
(
[category_id] => 15
[category_name] => Топы
[parent_id] => 6
[уровень] => 3
)

[15] => Массив
(
[category_id] => 16
[category_name] => Кофты
[parent_id] => 8
[уровень] => 4
)

[16] => Массив
(
[category_id] => 17
[category_name] => Аксессуары для одежды
[parent_id] => 5
[уровень] => 2
)

[17] => Массив
(
[category_id] => 18
[category_name] => Шляпы
[parent_id] => 17
[уровень] => 3
)

[18] => Массив
(
[category_id] => 19
[category_name] => Рубашки
[parent_id] => 15
[уровень] => 4
)

[19] => Массив
(
[category_id] => 20
[category_name] => Брюки-карго
[parent_id] => 13
[уровень] => 4
)

[20] => Массив
(
[category_id] => 21
[category_name] => Нижнее белье  Носки
[parent_id] => 6
[уровень] => 3
)

[21] => Массив
(
[category_id] => 22
[category_name] => Носки
[parent_id] => 21
[уровень] => 4
)

[22] => Массив
(
[category_id] => 23
[category_name] => Рубашки Поло
[parent_id] => 8
[уровень] => 4
)

[23] => Массив
(
[category_id] => 24
[category_name] => Одежда  аксессуары
[parent_id] => 0
[уровень] => 1
)

[24] => Массив
(
[category_id] => 25
[category_name] => Аксессуары для одежды
[parent_id] => 24
[уровень] => 2
)

[25] => Массив
(
[category_id] => 26
[category_name] => Ремни
[parent_id] => 25
[уровень] => 3
)

[26] => Массив
(
[category_id] => 27
[category_name] => Перчатки  Рукавицы
[parent_id] => 17
[уровень] => 3
)

[27] => Массив
(
[category_id] => 28
[category_name] => Перчатки
[parent_id] => 27
[уровень] => 4
)

[28] => Массив
(
[category_id] => 29
[category_name] => Обувь
[parent_id] => 24
[уровень] => 2
)

[29] => Массив
(
[category_id] => 30
[category_name] => Сандалии
[parent_id] => 29
[уровень] => 3
)

[30] => Массив
(
[category_id] => 31
[category_name] => Стринги  Шлепки
[parent_id] => 30
[уровень] => 4
)

[31] => Массив
(
[category_id] => 32
[category_name] => Камера  мешки
[parent_id] => 0
[уровень] => 1
)

[32] => Массив
(
[category_id] => 33
[category_name] => Сумки Messenger
[parent_id] => 32
[уровень] => 2
)

[33] => Массив
(
[category_id] => 34
[category_name] => Джинсы
[parent_id] => 13
[уровень] => 4
)

[34] => Массив
(
[category_id] => 35
[category_name] => Одежда
[parent_id] => 24
[уровень] => 2
)

[35] => Массив
(
[category_id] => 36
[category_name] => Брюки
[parent_id] => 35
[уровень] => 3
)

[36] => Массив
(
[category_id] => 37
[category_name] => Джинсы
[parent_id] => 36
[уровень] => 4
)

[37] => Массив
(
[category_id] => 38
[category_name] => Сумки, кошельки  случаи
[parent_id] => 5
[уровень] => 2
)

[38] => Массив
(
[category_id] => 39
[category_name] => Кошельки  Зажимы для денег
[parent_id] => 38
[уровень] => 3
)

[39] => Массив
(
[category_id] => 40
[category_name] => Бейсболки
[parent_id] => 18
[уровень] => 4
)

[40] => Массив
(
[category_id] => 41
[category_name] => Сумочка  Аксессуары для кошелька
[parent_id] => 5
[уровень] => 2
)

[41] => Массив
(
[category_id] => 42
[category_name] => Ключевые цепочки
[parent_id] => 41
[уровень] => 3
)

[42] => Массив
(
[category_id] => 43
[category_name] => Обувь
[parent_id] => 5
[уровень] => 2
)

[43] => Массив
(
[category_id] => 44
[category_name] => Сапоги
[parent_id] => 43
[уровень] => 3
)

[44] => Массив
(
[category_id] => 45
[category_name] => Водонепроницаемые ботинки  высокие сапоги
[parent_id] => 44
[уровень] => 4
)

[45] => Массив
(
[category_id] => 46
[category_name] => Barware
[parent_id] => 2
[уровень] => 3
)

[46] => Массив
(
[category_id] => 47
[category_name] => Шейкеры с напитками  инструменты
[parent_id] => 46
[уровень] => 4
)

[47] => Массив
(
[category_id] => 48
[category_name] => Открывашки
[parent_id] => 47
[уровень] => 5
)

[48] ​​=> Массив
(
[category_id] => 49
[category_name] => Посуда
[parent_id] => 2
[уровень] => 3
)

[49] => Массив
(
[category_id] => 50
[category_name] => Питьевая посуда
[parent_id] => 49
[уровень] => 4
)

[50] => Массив
(
[category_id] => 51
[category_name] => Кружки
[parent_id] => 50
[уровень] => 5
)

[51] => Массив
(
[category_id] => 52
[category_name] => Верхняя одежда
[parent_id] => 6
[уровень] => 3
)

[52] => Массив
(
[category_id] => 53
[category_name] => Пальто  Жакеты
[parent_id] => 52
[уровень] => 4
)

[53] => Массив
(
[category_id] => 54
[category_name] => Пуховики
[parent_id] => 53
[уровень] => 5
)

[54] => Массив
(
[category_id] => 55
[category_name] => Рубашки  верхушки
[parent_id] => 35
[уровень] => 3
)

[55] => Массив
(
[category_id] => 56
[category_name] => Футболки
[parent_id] => 55
[уровень] => 4
)

[56] => Массив
(
[category_id] => 57
[category_name] => Футболки
[parent_id] => 15
[уровень] => 4
)

[57] => Массив
(
[category_id] => 58
[category_name] => Сандалии
[parent_id] => 43
[уровень] => 3
)

[58] => Массив
(
[category_id] => 59
[category_name] => Стринги  Шлепки
[parent_id] => 58
[уровень] => 4
)

[59] => Массив
(
[category_id] => 60
[category_name] => Ремни
[parent_id] => 17
[уровень] => 3
)

[60] => Массив
(
[category_id] => 61
[category_name] => Грузовые шорты
[parent_id] => 7
[уровень] => 4
)

[61] => Массив
(
[category_id] => 62
[category_name] => Перемычки  Cardigans
[parent_id] => 15
[уровень] => 4
)

[62] => Массив
(
[category_id] => 63
[category_name] => Косметика  Сумки для туалетных принадлежностей
[parent_id] => 32
[уровень] => 2
)

[63] => Массив
(
[category_id] => 64
[category_name] => Активная одежда
[parent_id] => 35
[уровень] => 3
)

[64] => Массив
(
[category_id] => 65
[category_name] => Кофты
[parent_id] => 64
[уровень] => 4
)

[65] => Массив
(
[category_id] => 66
[category_name] => Солнечные шляпы
[parent_id] => 18
[уровень] => 4
)

[66] => Массив
(
[category_id] => 67
[category_name] => Флисовые куртки
[parent_id] => 53
[уровень] => 5
)

[67] => Массив
(
[category_id] => 68
[category_name] => Гилеты
[parent_id] => 52
[уровень] => 4
)

[68] => Массив
(
[category_id] => 69
[category_name] => Сумки
[parent_id] => 38
[уровень] => 3
)

[69] => Массив
(
[category_id] => 70
[category_name] => Сумки Tote  Покупатели
[parent_id] => 69
[уровень] => 4
)

[70] => Массив
(
[category_id] => 71
[category_name] => Уличная обувь
[parent_id] => 29
[уровень] => 3
)

[71] => Массив
(
[category_id] => 72
[category_name] => Водные туфли
[parent_id] => 71
[уровень] => 4
)

[72] => Массив
(
[category_id] => 73
[category_name] => Рюкзаки
[parent_id] => 32
[уровень] => 2
)

[73] => Массив
(
[category_id] => 74
[category_name] => Рубашки  Блузы
[parent_id] => 55
[уровень] => 4
)

[74] => Массив
(
[category_id] => 75
[category_name] => Шарфы  Snoods
[parent_id] => 17
[уровень] => 3
)

[75] => Массив
(
[category_id] => 76
[category_name] => Чемоданы
[parent_id] => 32
[уровень] => 2
)

[76] => Массив
(
[category_id] => 77
[category_name] => Ручная кладь
[parent_id] => 76
[уровень] => 3
)

[77] => Массив
(
[category_id] => 78
[category_name] => Жилеты  Майки
[parent_id] => 8
[уровень] => 4
)

[78] => Массив
(
[category_id] => 79
[category_name] => Зонтики  Зонтики
[parent_id] => 1
[уровень] => 2
)

[79] => Массив
(
[category_id] => 80
[category_name] => Ручная кладь
[parent_id] => 76
[уровень] => 3
)

[80] => Массив
(
[category_id] => 81
[category_name] => Платья
[parent_id] => 6
[уровень] => 3
)

[81] => Массив
(
[category_id] => 82
[category_name] => Дневные платья
[parent_id] => 81
[уровень] => 4
)

[82] => Массив
(
[category_id] => 83
[category_name] => Брюки-карго
[parent_id] => 13
[уровень] => 4
)

[83] => Массив
(
[category_id] => 84
[category_name] => Ручная кладь
[parent_id] => 32
[уровень] => 2
)

[84] => Массив
(
[category_id] => 85
[category_name] => Сумки через плечо
[parent_id] => 69
[уровень] => 4
)

[85] => Массив
(
[category_id] => 86
[category_name] => Бермудские шорты
[parent_id] => 7
[уровень] => 4
)

[86] => Массив
(
[category_id] => 87
[category_name] => Шорты
[parent_id] => 35
[уровень] => 3
)

[87] => Массив
(
[category_id] => 88
[category_name] => Грузовые шорты
[parent_id] => 87
[уровень] => 4
)

[88] => Массив
(
[category_id] => 89
[category_name] => Брюки
[parent_id] => 6
[уровень] => 3
)

[89] => Массив
(
[category_id] => 90
[category_name] => Повседневные брюки
[parent_id] => 89
[уровень] => 4
)

[90] => Массив
(
[category_id] => 91
[category_name] => Сумочка  Аксессуары для кошелька
[parent_id] => 24
[уровень] => 2
)

[91] => Массив
(
[category_id] => 92
[category_name] => Ремешки
[parent_id] => 91
[уровень] => 3
)

[92] => Массив
(
[category_id] => 93
[category_name] => вещевые сумки
[parent_id] => 32
[уровень] => 2
)

[93] => Массив
(
[category_id] => 94
[category_name] => Рубашки Поло
[parent_id] => 15
[уровень] => 4
)

[94] => Массив
(
[category_id] => 95
[category_name] => Skorts
[parent_id] => 6
[уровень] => 3
)

[95] => Массив
(
[category_id] => 96
[category_name] => Повседневные куртки
[parent_id] => 53
[уровень] => 5
)

[96] => Массив
(
[category_id] => 97
[category_name] => Леггинсы
[parent_id] => 89
[уровень] => 4
)

[97] => Массив
(
[category_id] => 98
[category_name] => Столовые
[parent_id] => 3
[уровень] => 4
)

[98] => Массив
(
[category_id] => 99
[category_name] => Заглавные буквы
[parent_id] => 18
[уровень] => 4
)

[99] => Массив
(
[category_id] => 100
[category_name] => Бейсболки
[parent_id] => 99
[уровень] => 5
)

[100] => Массив
(
[category_id] => 101
[category_name] => Rain Gear
[parent_id] => 52
[уровень] => 4
)

[101] => Массив
(
[category_id] => 102
[category_name] => Водонепроницаемые пальто
[parent_id] => 101
[уровень] => 5
)

[102] => Массив
(
[category_id] => 103
[category_name] => Накидки  Пончо
[parent_id] => 53
[уровень] => 5
)

[103] => Массив
(
[category_id] => 104
[category_name] => Кофты
[parent_id] => 15
[уровень] => 4
)

[104] => Массив
(
[category_id] => 105
[category_name] => Уличная обувь
[parent_id] => 43
[уровень] => 3
)

[105] => Массив
(
[category_id] => 106
[category_name] => Водные туфли
[parent_id] => 105
[уровень] => 4
)

[106] => Массив
(
[category_id] => 107
[category_name] => Рубашки Поло
[parent_id] => 55
[уровень] => 4
)

[107] => Массив
(
[category_id] => 108
[category_name] => Платья
[parent_id] => 35
[уровень] => 3
)

[108] => Массив
(
[category_id] => 109
[category_name] => Дневные платья
[parent_id] => 108
[уровень] => 4
)

[109] => Массив
(
[category_id] => 110
[category_name] => Свитера  Cardigans
[parent_id] => 55
[уровень] => 4
)

[110] => Массив
(
[category_id] => 111
[category_name] => Торговые сумки
[parent_id] => 32
[уровень] => 2
)

[111] => Массив
(
[category_id] => 112
[category_name] => Кофты
[parent_id] => 55
[уровень] => 4
)

[112] => Массив
(
[category_id] => 113
[category_name] => Шарфы  Платки
[parent_id] => 25
[уровень] => 3
)

[113] => Массив
(
[category_id] => 114
[category_name] => Сумки, кошельки  случаи
[parent_id] => 24
[уровень] => 2
)

[114] => Массив
(
[category_id] => 115
[category_name] => Сумки
[parent_id] => 114
[уровень] => 3
)

[115] => Массив
(
[category_id] => 116
[category_name] => Сумки Tote
[parent_id] => 115
[уровень] => 4
)

[116] => Массив
(
[category_id] => 117
[category_name] => Нижнее белье  Носки
[parent_id] => 35
[уровень] => 3
)

[117] => Массив
(
[category_id] => 118
[category_name] => Носки
[parent_id] => 117
[уровень] => 4
)

[118] => Массив
(
[category_id] => 119
[category_name] => Сумки через плечо
[parent_id] => 115
[уровень] => 4
)

[119] => Массив
(
[category_id] => 120
[category_name] => Нижнее белье
[parent_id] => 117
[уровень] => 4
)

[120] => Массив
(
[category_id] => 121
[category_name] => Верхняя одежда
[parent_id] => 35
[уровень] => 3
)

[121] => Массив
(
[category_id] => 122
[category_name] => Пальто  Жакеты
[parent_id] => 121
[уровень] => 4
)

[122] => Массив
(
[category_id] => 123
[category_name] => Камзолы  Майки
[parent_id] => 55
[уровень] => 4
)

[123] => Массив
(
[category_id] => 124
[category_name] => Юбки
[parent_id] => 35
[уровень] => 3
)

[124] => Массив
(
[category_id] => 125
[category_name] => Флисовые куртки
[parent_id] => 122
[уровень] => 5
)

[125] => Массив
(
[category_id] => 126
[category_name] => Хаки  Брюки чино
[parent_id] => 36
[уровень] => 4
)

[126] => Массив
(
[category_id] => 127
[category_name] => Брюки-карго
[parent_id] => 36
[уровень] => 4
)

[127] => Массив
(
[category_id] => 128
[category_name] => Шляпы
[parent_id] => 25
[уровень] => 3
)

[128] => Массив
(
[category_id] => 129
[category_name] => Шапочки
[parent_id] => 128
[уровень] => 4
)

[129] => Массив
(
[category_id] => 130
[category_name] => Перчатки  Рукавицы
[parent_id] => 25
[уровень] => 3
)

[130] => Массив
(
[category_id] => 131
[category_name] => Шарфы  Платки
[parent_id] => 17
[уровень] => 3
)

[131] => Массив
(
[category_id] => 132
[category_name] => Брюки
[parent_id] => 6
[уровень] => 3
)

[132] => Массив
(
[category_id] => 133
[category_name] => Хаки  Брюки чино
[parent_id] => 132
[уровень] => 4
)

[133] => Массив
(
[category_id] => 134
[category_name] => Сумки Tote
[parent_id] => 69
[уровень] => 4
)

[134] => Массив
(
[category_id] => 135
[category_name] => Камзолы  Майки
[parent_id] => 8
[уровень] => 4
)

[135] => Массив
(
[category_id] => 136
[category_name] => Джинсы
[parent_id] => 132
[уровень] => 4
)

[136] => Массив
(
[category_id] => 137
[category_name] => Свитера  Cardigans
[parent_id] => 8
[уровень] => 4
)

[137] => Массив
(
[category_id] => 138
[category_name] => Брюки-карго
[parent_id] => 132
[уровень] => 4
)

[138] => Массив
(
[category_id] => 139
[category_name] => Шапочки
[parent_id] => 18
[уровень] => 4
)

[139] => Массив
(
[category_id] => 140
[category_name] => Зонтики  Дождевые зонтики
[parent_id] => 1
[уровень] => 2
)

[140] => Массив
(
[category_id] => 141
[category_name] => Ремешки
[parent_id] => 41
[уровень] => 3
)

[141] => Массив
(
[category_id] => 142
[category_name] => Сумки через плечо
[parent_id] => 69
[уровень] => 4
)

[142] => Массив
(
[category_id] => 143
[category_name] => Хаки  Брюки чино
[parent_id] => 13
[уровень] => 4
)

[143] => Массив
(
[category_id] => 144
[category_name] => Нижнее белье
[parent_id] => 21
[уровень] => 4
)

)

Как преобразовать этот массив, похожий на этот:

массив
(
[0] => Массив
(
[0] => Домой  Сад
[1] => Кухня  обеденный
[2] => Еда  Перевозчики напитков
[3] => Колбы
[4] =>
)

[1] => Массив
(
[0] =>
[1] =>
[2] =>
[3] => Столовые
[4] =>
)

[2] => Массив
(
[0] =>
[1] =>
[2] => Посуда
[3] => Шейкеры с напитками  инструменты
[4] => Открывашки
)

[3] => Массив
(
[0] =>
[1] =>
[2] => Посуда
[3] => Питьевая посуда
[4] => Кружки
)

[4] => Массив
(
[0] =>
[1] => Зонтики  Зонтики
[2] =>
[3] =>
[4] =>
)

[5] => Массив
(
[0] =>
[1] => Зонтики  Дождевые зонтики
[2] =>
[3] =>
[4] =>
)

[6] => Массив
(
[0] => Одежда  аксессуары
[1] => Одежда
[2] => Шорты
[3] => Шорты-карго
[4] =>
)

)

Родительское дочернее представление для справки:

введите описание изображения здесь

Есть идеи???

0

Решение

Очень интересное задание. Смотрите мое решение ниже:

массив категорий

$categoriesrRaw = Array
(
'0' => Array
(
'category_id' => 1,
'category_name' => 'Home & Garden',
'parent_id' => 0,
'level' => 1,
),

'1' => Array
(
'category_id' => 2,
'category_name' => 'Kitchen & Dining',
'parent_id' => 1,
'level' => 2,
),

'2' => Array
(
'category_id' => 3,
'category_name' => 'Food & Beverage Carriers',
'parent_id' => 2,
'level' => 3,
),

'3' => Array
(
'category_id' => 4,
'category_name' => 'Flasks',
'parent_id' => 3,
'level' => 4,
),

'4' => Array
(
'category_id' => 5,
'category_name' => 'Clothing & Accessories',
'parent_id' => 0,
'level' => 1,
),

'5' => Array
(
'category_id' => 6,
'category_name' => 'Clothing',
'parent_id' => 5,
'level' => 2,
),

'6' => Array
(
'category_id' => 7,
'category_name' => 'Shorts',
'parent_id' => 6,
'level' => 3,
),

'7' => Array
(
'category_id' => 8,
'category_name' => 'Shirts & Tops',
'parent_id' => 6,
'level' => 3,
),

'8' => Array
(
'category_id' => 9,
'category_name' => 'Jumpers & Cardigans',
'parent_id' => 8,
'level' => 4,
),

'9' => Array
(
'category_id' => 10,
'category_name' => 'T-Shirts',
'parent_id' => 8,
'level' => 4,
),

'10' => Array
(
'category_id' => 11,
'category_name' => 'Skirts',
'parent_id' => 6,
'level' => 3,
),

'11' => Array
(
'category_id' => 12,
'category_name' => 'Shirts & Blouses',
'parent_id' => 8,
'level' => 4,
),

'12' => Array
(
'category_id' => 13,
'category_name' => 'Trousers & Jeans',
'parent_id' => 6,
'level' => 3,
),

'13' => Array
(
'category_id' => 14,
'category_name' => 'Chinos',
'parent_id' => 13,
'level' => 4,
),

'14' => Array
(
'category_id' => 15,
'category_name' => 'Tops',
'parent_id' => 6,
'level' => 3,
),

'15' => Array
(
'category_id' => 16,
'category_name' => 'Sweatshirts',
'parent_id' => 8,
'level' => 4,
),

'16' => Array
(
'category_id' => 17,
'category_name' => 'Clothing Accessories',
'parent_id' => 5,
'level' => 2,
),

'17' => Array
(
'category_id' => 18,
'category_name' => 'Hats',
'parent_id' => 17,
'level' => 3,
),

'18' => Array
(
'category_id' => 19,
'category_name' => 'Shirts',
'parent_id' => 15,
'level' => 4,
),

'19' => Array
(
'category_id' => 20,
'category_name' => 'Cargo Trousers',
'parent_id' => 13,
'level' => 4,
),

'20' => Array
(
'category_id' => 21,
'category_name' => 'Underwear & Socks',
'parent_id' => 6,
'level' => 3,
),

'21' => Array
(
'category_id' => 22,
'category_name' => 'Socks',
'parent_id' => 21,
'level' => 4,
),

'22' => Array
(
'category_id' => 23,
'category_name' => 'Polo Shirts',
'parent_id' => 8,
'level' => 4,
),

'23' => Array
(
'category_id' => 24,
'category_name' => 'Apparel & Accessories',
'parent_id' => 0,
'level' => 1,
),

'24' => Array
(
'category_id' => 25,
'category_name' => 'Clothing Accessories',
'parent_id' => 24,
'level' => 2,
),

'25' => Array
(
'category_id' => 26,
'category_name' => 'Belts',
'parent_id' => 25,
'level' => 3,
),

'26' => Array
(
'category_id' => 27,
'category_name' => 'Gloves & Mittens',
'parent_id' => 17,
'level' => 3,
),

'27' => Array
(
'category_id' => 28,
'category_name' => 'Gloves',
'parent_id' => 27,
'level' => 4,
),

'28' => Array
(
'category_id' => 29,
'category_name' => 'Shoes',
'parent_id' => 24,
'level' => 2,
),

'29' => Array
(
'category_id' => 30,
'category_name' => 'Sandals',
'parent_id' => 29,
'level' => 3,
),

'30' => Array
(
'category_id' => 31,
'category_name' => 'Thongs & Flip-Flops',
'parent_id' => 30,
'level' => 4,
),

'31' => Array
(
'category_id' => 32,
'category_name' => 'Luggage & Bags',
'parent_id' => 0,
'level' => 1,
),

'32' => Array
(
'category_id' => 33,
'category_name' => 'Messenger Bags',
'parent_id' => 32,
'level' => 2,
),

'33' => Array
(
'category_id' => 34,
'category_name' => 'Jeans',
'parent_id' => 13,
'level' => 4,
),

'34' => Array
(
'category_id' => 35,
'category_name' => 'Clothing',
'parent_id' => 24,
'level' => 2,
),

'35' => Array
(
'category_id' => 36,
'category_name' => 'Pants',
'parent_id' => 35,
'level' => 3,
),

'36' => Array
(
'category_id' => 37,
'category_name' => 'Jeans',
'parent_id' => 36,
'level' => 4,
),

'37' => Array
(
'category_id' => 38,
'category_name' => 'Handbags, Wallets & Cases',
'parent_id' => 5,
'level' => 2,
),

'38' => Array
(
'category_id' => 39,
'category_name' => 'Wallets & Money Clips',
'parent_id' => 38,
'level' => 3,
),

'39' => Array
(
'category_id' => 40,
'category_name' => 'Baseball Hats',
'parent_id' => 18,
'level' => 4,
),

'40' => Array
(
'category_id' => 41,
'category_name' => 'Handbag & Wallet Accessories',
'parent_id' => 5,
'level' => 2,
),

'41' => Array
(
'category_id' => 42,
'category_name' => 'Key Chains',
'parent_id' => 41,
'level' => 3,
),

'42' => Array
(
'category_id' => 43,
'category_name' => 'Shoes',
'parent_id' => 5,
'level' => 2,
),

'43' => Array
(
'category_id' => 44,
'category_name' => 'Boots',
'parent_id' => 43,
'level' => 3,
),

'44' => Array
(
'category_id' => 45,
'category_name' => 'Waterproof Boots & Wellingtons',
'parent_id' => 44,
'level' => 4,
),

'45' => Array
(
'category_id' => 46,
'category_name' => 'Barware',
'parent_id' => 2,
'level' => 3,
),

'46' => Array
(
'category_id' => 47,
'category_name' => 'Drink Shakers & Tools',
'parent_id' => 46,
'level' => 4,
),

'47' => Array
(
'category_id' => 48,
'category_name' => 'Bottle Openers',
'parent_id' => 47,
'level' => 5,
),

'48' => Array
(
'category_id' => 49,
'category_name' => 'Tableware',
'parent_id' => 2,
'level' => 3,
),

'49' => Array
(
'category_id' => 50,
'category_name' => 'Drinkware',
'parent_id' => 49,
'level' => 4,
),

'50' => Array
(
'category_id' => 51,
'category_name' => 'Mugs',
'parent_id' => 50,
'level' => 5,
),

'51' => Array
(
'category_id' => 52,
'category_name' => 'Outerwear',
'parent_id' => 6,
'level' => 3,
),

'52' => Array
(
'category_id' => 53,
'category_name' => 'Coats & Jackets',
'parent_id' => 52,
'level' => 4,
),

'53' => Array
(
'category_id' => 54,
'category_name' => 'Puffer jackets',
'parent_id' => 53,
'level' => 5,
),

'54' => Array
(
'category_id' => 55,
'category_name' => 'Shirts & Tops',
'parent_id' => 35,
'level' => 3,
),

'55' => Array
(
'category_id' => 56,
'category_name' => 'T-Shirts',
'parent_id' => 55,
'level' => 4,
),

'56' => Array
(
'category_id' => 57,
'category_name' => 'T-Shirts',
'parent_id' => 15,
'level' => 4,
),

'57' => Array
(
'category_id' => 58,
'category_name' => 'Sandals',
'parent_id' => 43,
'level' => 3,
),

'58' => Array
(
'category_id' => 59,
'category_name' => 'Thongs & Flip Flops',
'parent_id' => 58,
'level' => 4,
),

'59' => Array
(
'category_id' => 60,
'category_name' => 'Belts',
'parent_id' => 17,
'level' => 3,
),

'60' => Array
(
'category_id' => 61,
'category_name' => 'Cargo Shorts',
'parent_id' => 7,
'level' => 4,
),

'61' => Array
(
'category_id' => 62,
'category_name' => 'Jumpers & Cardigans',
'parent_id' => 15,
'level' => 4,
),

'62' => Array
(
'category_id' => 63,
'category_name' => 'Cosmetic & Toiletry Bags',
'parent_id' => 32,
'level' => 2,
),

'63' => Array
(
'category_id' => 64,
'category_name' => 'Activewear',
'parent_id' => 35,
'level' => 3,
),

'64' => Array
(
'category_id' => 65,
'category_name' => 'Sweatshirts',
'parent_id' => 64,
'level' => 4,
),

'65' => Array
(
'category_id' => 66,
'category_name' => 'Sun Hats',
'parent_id' => 18,
'level' => 4,
),

'66' => Array
(
'category_id' => 67,
'category_name' => 'Fleece Jackets',
'parent_id' => 53,
'level' => 5,
),

'67' => Array
(
'category_id' => 68,
'category_name' => 'Gilets',
'parent_id' => 52,
'level' => 4,
),

'68' => Array
(
'category_id' => 69,
'category_name' => 'Handbags',
'parent_id' => 38,
'level' => 3,
),

'69' => Array
(
'category_id' => 70,
'category_name' => 'Tote Bags & Shoppers',
'parent_id' => 69,
'level' => 4,
),

'70' => Array
(
'category_id' => 71,
'category_name' => 'Outdoor Shoes',
'parent_id' => 29,
'level' => 3,
),

'71' => Array
(
'category_id' => 72,
'category_name' => 'Water Shoes',
'parent_id' => 71,
'level' => 4,
),

'72' => Array
(
'category_id' => 73,
'category_name' => 'Backpacks',
'parent_id' => 32,
'level' => 2,
),

'73' => Array
(
'category_id' => 74,
'category_name' => 'Shirts & Blouses',
'parent_id' => 55,
'level' => 4,
),

'74' => Array
(
'category_id' => 75,
'category_name' => 'Scarves & Snoods',
'parent_id' => 17,
'level' => 3,
),

'75' => Array
(
'category_id' => 76,
'category_name' => 'Suitcases',
'parent_id' => 32,
'level' => 2,
),

'76' => Array
(
'category_id' => 77,
'category_name' => 'Hand Luggage',
'parent_id' => 76,
'level' => 3,
),

'77' => Array
(
'category_id' => 78,
'category_name' => 'Vests & Tank Tops',
'parent_id' => 8,
'level' => 4,
),

'78' => Array
(
'category_id' => 79,
'category_name' => 'Parasols & Umbrellas',
'parent_id' => 1,
'level' => 2,
),

'79' => Array
(
'category_id' => 80,
'category_name' => 'Carry-On Luggage',
'parent_id' => 76,
'level' => 3,
),

'80' => Array
(
'category_id' => 81,
'category_name' => 'Dresses',
'parent_id' => 6,
'level' => 3,
),

'81' => Array
(
'category_id' => 82,
'category_name' => 'Day Dresses',
'parent_id' => 81,
'level' => 4,
),

'82' => Array
(
'category_id' => 83,
'category_name' => 'Cargo Pants',
'parent_id' => 13,
'level' => 4,
),

'83' => Array
(
'category_id' => 84,
'category_name' => 'Hand Luggage',
'parent_id' => 32,
'level' => 2,
),

'84' => Array
(
'category_id' => 85,
'category_name' => 'Shoulder Bags',
'parent_id' => 69,
'level' => 4,
),

'85' => Array
(
'category_id' => 86,
'category_name' => 'Bermuda Shorts',
'parent_id' => 7,
'level' => 4,
),

'86' => Array
(
'category_id' => 87,
'category_name' => 'Shorts',
'parent_id' => 35,
'level' => 3,
),

'87' => Array
(
'category_id' => 88,
'category_name' => 'Cargo Shorts',
'parent_id' => 87,
'level' => 4,
),

'88' => Array
(
'category_id' => 89,
'category_name' => 'Trousers',
'parent_id' => 6,
'level' => 3,
),

'89' => Array
(
'category_id' => 90,
'category_name' => 'Casual Trousers',
'parent_id' => 89,
'level' => 4,
),

'90' => Array
(
'category_id' => 91,
'category_name' => 'Handbag & Wallet Accessories',
'parent_id' => 24,
'level' => 2,
),

'91' => Array
(
'category_id' => 92,
'category_name' => 'Lanyards',
'parent_id' => 91,
'level' => 3,
),

'92' => Array
(
'category_id' => 93,
'category_name' => 'Duffel Bags',
'parent_id' => 32,
'level' => 2,
),

'93' => Array
(
'category_id' => 94,
'category_name' => 'Polo Shirts',
'parent_id' => 15,
'level' => 4,
),

'94' => Array
(
'category_id' => 95,
'category_name' => 'Skorts',
'parent_id' => 6,
'level' => 3,
),

'95' => Array
(
'category_id' => 96,
'category_name' => 'Casual Jackets',
'parent_id' => 53,
'level' => 5,
),

'96' => Array
(
'category_id' => 97,
'category_name' => 'Leggings',
'parent_id' => 89,
'level' => 4,
),

'97' => Array
(
'category_id' => 98,
'category_name' => 'Canteens',
'parent_id' => 3,
'level' => 4,
),

'98' => Array
(
'category_id' => 99,
'category_name' => 'Caps',
'parent_id' => 18,
'level' => 4,
),

'99' => Array
(
'category_id' => 100,
'category_name' => 'Baseball Hats',
'parent_id' => 99,
'level' => 5,
),

'100' => Array
(
'category_id' => 101,
'category_name' => 'Rain Gear',
'parent_id' => 52,
'level' => 4,
),

'101' => Array
(
'category_id' => 102,
'category_name' => 'Waterproof Coats',
'parent_id' => 101,
'level' => 5,
),

'102' => Array
(
'category_id' => 103,
'category_name' => 'Capes & Ponchos',
'parent_id' => 53,
'level' => 5,
),

'103' => Array
(
'category_id' => 104,
'category_name' => 'Sweatshirts',
'parent_id' => 15,
'level' => 4,
),

'104' => Array
(
'category_id' => 105,
'category_name' => 'Outdoor Shoes',
'parent_id' => 43,
'level' => 3,
),

'105' => Array
(
'category_id' => 106,
'category_name' => 'Water Shoes',
'parent_id' => 105,
'level' => 4,
),

'106' => Array
(
'category_id' => 107,
'category_name' => 'Polo Shirts',
'parent_id' => 55,
'level' => 4,
),

'107' => Array
(
'category_id' => 108,
'category_name' => 'Dresses',
'parent_id' => 35,
'level' => 3,
),

'108' => Array
(
'category_id' => 109,
'category_name' => 'Day Dresses',
'parent_id' => 108,
'level' => 4,
),

'109' => Array
(
'category_id' => 110,
'category_name' => 'Sweaters & Cardigans',
'parent_id' => 55,
'level' => 4,
),

'110' => Array
(
'category_id' => 111,
'category_name' => 'Shopping Totes',
'parent_id' => 32,
'level' => 2,
),

'111' => Array
(
'category_id' => 112,
'category_name' => 'Sweatshirts',
'parent_id' => 55,
'level' => 4,
),

'112' => Array
(
'category_id' => 113,
'category_name' => 'Scarves & Shawls',
'parent_id' => 25,
'level' => 3,
),

'113' => Array
(
'category_id' => 114,
'category_name' => 'Handbags, Wallets & Cases',
'parent_id' => 24,
'level' => 2,
),

'114' => Array
(
'category_id' => 115,
'category_name' => 'Handbags',
'parent_id' => 114,
'level' => 3,
),

'115' => Array
(
'category_id' => 116,
'category_name' => 'Tote Handbags',
'parent_id' => 115,
'level' => 4,
),

'116' => Array
(
'category_id' => 117,
'category_name' => 'Underwear & Socks',
'parent_id' => 35,
'level' => 3,
),

'117' => Array
(
'category_id' => 118,
'category_name' => 'Socks',
'parent_id' => 117,
'level' => 4,
),

'118' => Array
(
'category_id' => 119,
'category_name' => 'Cross-Body Handbags',
'parent_id' => 115,
'level' => 4,
),

'119' => Array
(
'category_id' => 120,
'category_name' => 'Underwear',
'parent_id' => 117,
'level' => 4,
),

);

код

function categoriesCmd($a, $b)
{
return strnatcmp($a['path'], $b['path']);
}

//
// sort all categories values by path. Path - it is category parent ids:
// 1
// 1/2
// 1/2/3
// 1/2/3/4
// 1/2/3/98
// 1/2/46
// 1/2/46/47
// 1/2/46/47/48
// 1/2/49
// etc
//
$maxLevel   = 0;
$categories = array();
foreach ($categoriesrRaw as $key => $category) {
$parentId = $category['parent_id'];
$category['path']  = array($category['category_id']);
if ($maxLevel < $category['level']) {
$maxLevel = $category['level'];
}
while ($parentId) {
foreach ($categoriesrRaw as $item) {
if ($item['category_id'] == $parentId) {
$newParentId = $item['parent_id'];
$category['path'][] = $item['category_id'];
break;
}
}
$parentId = $newParentId;
// $category['level']++;
}
$category['path'] = implode('/', array_reverse($category['path']));
$categories[]    = $category;
}
usort($categories, 'categoriesCmd');//
// create needed array structure
//
$prev         = array_shift($categories);
$level        = 0;
$result       = array(array_fill(0, $maxLevel, ""));
$result[0][0] = $category['category_name'];
foreach ($categories as $category) {
if ($prev['level'] >= $category['level']) {
$level++;
$result[$level] = array_fill(0, $maxLevel, "");
}

$result[$level][$category['level']-1] = $category['category_name'];
$prev = $category;
}

print_r($result);
1

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]