Skip to content

Instantly share code, notes, and snippets.

@VictorFursa
Created December 16, 2015 20:24
Show Gist options
  • Save VictorFursa/e957c9bc41290f5dc7f9 to your computer and use it in GitHub Desktop.
Save VictorFursa/e957c9bc41290f5dc7f9 to your computer and use it in GitHub Desktop.
<?php
//phpinfo(); exit;
header('Content-Type: text/html; charset=utf-8');
$a = array(
array(
"id" => 1,
"parent_id" => 0,
"name" => "Электротовары"
),
array(
"id" => 2,
"parent_id" => 1,
"name" => "Компьтеры"
),
array(
"id" => 3,
"parent_id" => 1,
"name" => "Бытовая техника"
),
array(
"id" => 4,
"parent_id" => 2,
"name" => "Acer XD5732"
),
array(
"id" => 5,
"parent_id" => 3,
"name" => "Холодильник"
),
array(
"id" => 6,
"parent_id" => 3,
"name" => "Микроволновка"
),
array(
"id" => 7,
"parent_id" => 0,
"name" => "Автомобили"
),
array(
"id" => 8,
"parent_id" => 7,
"name" => "Pego 206"
)
);
function get_child($id, $data,$i){
$childrens = array();
foreach($data as $row){
if($row['parent_id'] == $id){
echo $row["name"] . "<br>";
$arr = array(
"name" => $row["name"],
"childrens" => get_child($row['id'], $data,$i)
);
$childrens[] = $arr;
}
elseif($row['parent_id'] >= 0 ){
echo $i;
/* можно просто echo "-" */
}
}
return $childrens;
}
get_child(0, $a,"-");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment