Skip to content

Instantly share code, notes, and snippets.

@uberbrady
Created October 2, 2019 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uberbrady/72ba93aea1bacd5007ef107a4b944078 to your computer and use it in GitHub Desktop.
Save uberbrady/72ba93aea1bacd5007ef107a4b944078 to your computer and use it in GitHub Desktop.
function indenter($locations, $parent_id = null, $prefix = '') {
$results = Array();
foreach($locations as $location) {
if($location->parent_id == $parent_id) {
//append this parent node first,
$results[] = [
'id' => $location->id,
'text' => $prefix.' '.$location->name,
'image' => $location->image
]
//now append the children.
$results = array_merge($results,indenter($locations,$location->id,$prefix.'--'));
}
}
return $results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment