Skip to content

Instantly share code, notes, and snippets.

@maxchene
Created April 17, 2014 12:25
Show Gist options
  • Save maxchene/10979382 to your computer and use it in GitHub Desktop.
Save maxchene/10979382 to your computer and use it in GitHub Desktop.
json array to flatten tree for database
public $actsAs = array('Tree');
public function flattenJsonTree($JSON_array, $iParentId = null, $iLevel = null)
{
$aRetval = array();
$iPosition = 1;
foreach ($JSON_array as $aChilds) {
$aDescendents = array();
if (isset($aChilds['children'])) {
$aDescendents = $this->flattenJsonTree(
$aChilds['children'], $aChilds['id'], $iLevel+1
);
}
$aRetval[] = array(
'id' => $aChilds['id'],
'parent_id' => $iParentId,
//'level' => $iLevel,
'order' => $iPosition++,
);
$aRetval = array_merge($aRetval, $aDescendents);
}
return $aRetval;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment