I'm trying to build an array with this structure. I have all the data on one single intermediate table which contains all the keys (if you see the code below it will be better understood). Basically I need to get this structure of array because I want to build a tree view.
10
|---14
| |---17
| | |---20
| | | |---11
| | | |---12
| | |
| | |---21
| | |---11
| | |---13
| |---18
| |---30
| | |---11
| | |---14
| |
| |---31
| |---15
| |---16
|---15
|---16
| |---40
| | |---11
| | |---12
| |
| |---41
| |---13
| |---14
|
|---19
|---42
| |---11
| |---12
|
|---43
|---13
|---14
Of which the logic is as follow
$country_id = 10;
$cities = DB::table('table_general')->select('city_id')->where('country_id', $country_id)->distinct()->get();
foreach($cities as $city){
$departments = DB::table('table_general')->select('depart_id')->where('city_id', $city->id)
->where('country_id', $country_id)->distinct()->get();
foreach($departments as $departament){
$provinces = DB::table('table_general')->select('province_id')->where('depart_id', $departament->id)
->where('city_id', $city->id)
->where('country_id', $country_id)->distinct()->get();
foreach($provinces as $province){
$districts = DB::table('table_general')->select('district_id')->where('province_id', $province->id)
->where('depart_id', $departament->id)
->where('city_id', $city->id)
->where('country_id', $country_id)
->distinct()->get();
foreach($districts as $district){
// I've tried something like this to build the array
// But isn't the correct way
$array = array();
$array["country"] = $country_id;
$array["country"][]["city"][] = $city->id;
$array = array("country" => $country_id, array("city" => $city->id));
}
}
}
}
How should I build with the best way my arrays to build an array like the example above?