Skip to content

Instantly share code, notes, and snippets.

@iBasit
Created June 14, 2017 18:10
Show Gist options
  • Save iBasit/b3ce5537aab69e96d0943cc1d35d4df2 to your computer and use it in GitHub Desktop.
Save iBasit/b3ce5537aab69e96d0943cc1d35d4df2 to your computer and use it in GitHub Desktop.
$regions = array();
$regions[65] = array('id' => 65, 'parent_id' => 44, 'name' => "Shoreditch");
$regions[77] = array('id' => 77, 'parent_id' => 5, 'name' => "England");
$regions[100] = array('id' => 100, 'parent_id' => 0, 'name' => "Europe");
$regions[5] = array('id' => 5, 'parent_id' => 100, 'name' => "United Kingdom");
$regions[44] = array('id' => 44, 'parent_id' => 77, 'name' => "London");
function getChild ($parent_id = 0)
{
global $regions;
$value = '';
foreach ($regions as $location)
{
if ($location['parent_id'] == 0)
{
echo $location['name'].', ';
getChild($locatoin['id']); // print child
}
}
}
getChild(0); // start with parent_id 0;
// this should print — Europe, United Kingdom, England, London, Shoreditch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment