<?php
$catalog = [
[
'id' => 1,
'p_id' => '',
'name' => 'Parrent Name'
],
[
'id' => 2,
'p_id' => '',
'name' => 'Parrent Name'
],
[
'id' => 3,
'p_id' => '',
'name' => 'Parrent Name'
],
[
'id' => 4,
'p_id' => '1',
'name' => 'Parrent Name'
],
[
'id' => 5,
'p_id' => '2',
'name' => 'Parrent Name'
],
[
'id' => 6,
'p_id' => '3',
'name' => 'Parrent Name'
],
[
'id' => 7,
'p_id' => '4',
'name' => 'Parrent Name'
],
[
'id' => 8,
'p_id' => '5',
'name' => 'Parrent Name'
],
[
'id' => 9,
'p_id' => '5',
'name' => 'Parrent Name'
],
[
'id' => 10,
'p_id' => '9',
'name' => 'Parrent Name'
],
];
function recursive($data, $p_id = 0, $level = 0)
{
echo "<ul>";
foreach ($data as $category) {
echo "<li>";
if ($category['p_id'] == $p_id) {
$str = '';
if ($level > 0) {
for ($i=0; $i < $level; $i++) {
$str .= '-';
}
}
if ($category['p_id'] == $p_id) {
echo $category['name'] . "<br>\n";
}
$id = $category['id'];
$level = $level + 1;
recursive($data, $id, $level);
}
echo "</li>";
}
echo "</ul>";
}