Created
April 17, 2014 12:25
-
-
Save maxchene/10979382 to your computer and use it in GitHub Desktop.
json array to flatten tree for database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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