I'm trying to create a recursive method to fill an array with each new item found.
function getUserUbigeoString($ubigeo_id){
$ubigeoRepository = new \App\Repositories\UbigeoRepository();
$ubigeos = array();
$ubigeo = $ubigeoRepository->getUbigeo($ubigeo_id);
if(!empty($ubigeo)){
if($ubigeo->ubigeo_id == null){
$ubigeos[] = $ubigeo->name;
}
$ubigeos[] = getUserUbigeoString($ubigeo->ubigeo_id);
}
return $ubigeos;
}
The objective of the code is to get an array fill with all the name of the ubigeos.
0 => ubigeo1
1 => ubigeo2
2 => ubigeo3
etc...
As of right now, i have tried placing the return many different locations, but the closest result i have gotten was:
array:1 [▼
0 => array:1 [▼
0 => array:2 [▼
0 => "Port Dusty"
1 => []
]
]
]
==========EDIT============ Structure of database ubigeos:
id name level ubigeo_id
----------------------------
3 ubigeo1 1 null
37 ubigeo2 2 3
55 ubigeo3 3 37
the output would be a simple array like so, which then i could implode into a comma separated string:
array:1 [
0 => 'ubigeo1'
1 => 'ubigeo2'
2 => 'ubigeo3'
]
getUserUbigeoString()
– Aman Rawat Aug 12 at 6:27