Skip to content

Instantly share code, notes, and snippets.

@VictorFursa
Created January 4, 2016 11:13
Show Gist options
  • Select an option

  • Save VictorFursa/c46c2a77468ef08f8c75 to your computer and use it in GitHub Desktop.

Select an option

Save VictorFursa/c46c2a77468ef08f8c75 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" => 331,
"parent_id" => 0,
"name" => "Одежда"
),
array(
"id" => 2,
"parent_id" => 1,
"name" => "Компьтеры"
),
array(
"id" => 3,
"parent_id" => 0,
"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" => 3331,
"parent_id" => 331,
"name" => "Женская"
),
array(
"id" => 8,
"parent_id" => 7,
"name" => "Pego 206"
)
);
function get_child($id, $data) {
$childrens = array();
foreach($data as $row) {
if ($row['parent_id'] == $id) {
echo "<select>";
echo "<option " . " value=' " . $id . " ' > " . $row['name']."</option>";
echo "</select>";
$arr = array(
"name" => $row["name"],
"childrens" => get_child($row['id'], $data)
);
$childrens[] = $arr;
}
}
return $childrens;
}
$aa = get_child(0, $a);
echo "<pre>";
print_r($aa);
echo "</pre>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment