$answer = array(
array("id" => 1, "parent_id" => 100, "name" => "Стулья"),
array("id" => 2, "parent_id" => 100, "name" => "Столы"),
array("id" => 5, "parent_id" => 1, "name" => "Стул Сакура", "price" => 5691, "img" => "lorempixel.com/900/900/?q=5", "props" => array()),
array("id" => 9, "parent_id" => 2, "name" => "Стол Византия", "price" => 5268, "img" => "lorempixel.com/900/900/?q=9","props" => array())
);
$result = array();
foreach($answer as $item) {
if(isset($item['price'])) $result[$item['parent_id']][] = $item;
else $result[$item['id']] = $item;
}
var_dump($result);
<?php
$data = [
[
'id' => 1,
'parent_id' => 100,
'name' => 'Стулья'
], [
'id' => 2,
'parent_id' => 100,
'name' => 'Столы'
], [
'id' => 5,
'parent_id' => 1,
'name' => 'Стул Сакура',
'price' => 5691,
'img' => 'lorempixel.com/900/900/?q=5',
'props' => []
], [
'id' => 9,
'parent_id' => 2,
'name' => 'Стол Византия',
'price' => 5268,
'img' => 'lorempixel.com/900/900/?q=9',
'props' => []
]
];
function build_tree($data, $id) {
$result = [];
foreach ($data as $el) {
if ($el['parent_id'] == $id) {
$newEl = $el;
$childs = build_tree($data, $el['id']);
if (count($childs) !== 0) {
$newEl['childs'] = $childs;
}
$result[] = $newEl;
}
}
return $result;
}
$tree = build_tree($data, 100);
print_r($tree);
/*
Array (
[0] => Array (
[id] => 1
[parent_id] => 100
[name] => Стулья
[childs] => Array (
[0] => Array (
[id] => 5
[parent_id] => 1
[name] => Стул Сакура
[price] => 5691
[img] => lorempixel.com/900/900/?q=5
[props] => Array ()
)
)
)
[1] => Array (
[id] => 2
[parent_id] => 100
[name] => Столы
[childs] => Array (
[0] => Array (
[id] => 9
[parent_id] => 2
[name] => Стол Византия
[price] => 5268
[img] => lorempixel.com/900/900/?q=9
[props] => Array ()
)
)
)
)
*/