Skip to content

Instantly share code, notes, and snippets.

@uberbrady
Created October 2, 2019 09:39
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/98eca5dcf013c51858b90fe1478c53ad to your computer and use it in GitHub Desktop.
Save uberbrady/98eca5dcf013c51858b90fe1478c53ad to your computer and use it in GitHub Desktop.
$locations_with_children=[];
foreach($locations as $location) {
$locations_with_children[$location->parent_id] = true;
}
public static function indenter($locations, $locations_with_children, $parent_id = null, $prefix = '') {
$results = Array();
foreach($locations as $location) {
$locations_with_children[$location->parent_id] = true;
if($location->parent_id == $parent_id) {
//append this parent node first,
$location->use_text = $prefix.' '.$location->name;
$location->use_image = ($location->image) ? url('/').'/uploads/locations/'.$location->image : null;
$results[] = $location;
//now append the children. (if we have any)
if($locations_with_children[$location->id]) {
$results = array_merge($results,Location::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