Last active
February 26, 2020 04:38
-
-
Save ashokmhrj/515271f57e7f4328519c8b5f1669f4ef to your computer and use it in GitHub Desktop.
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
<?php | |
/*source: https://stackoverflow.com/questions/26848825/php-sorting-array-by-id-and-parent-id*/ | |
$arr = array( | |
array('id' => 1, 'parent' => 0, 'title' => 'XXX1', 'children'=>array()), | |
array('id' => 85, 'parent' => 0, 'title' => 'XXX2', 'children'=>array()), | |
array('id' => 41, 'parent' => 0, 'title' => 'XXX2', 'children'=>array()), | |
array('id' => 17, 'parent' => 0, 'title' => 'XXX3', 'children'=>array()), | |
array('id' => 66, 'parent' => 1, 'title' => 'XXX4', 'children'=>array()), | |
array('id' => 92, 'parent' => 1, 'title' => 'XXX5', 'children'=>array()), | |
array('id' => 65, 'parent' => 1, 'title' => 'XXX6', 'children'=>array()), | |
array('id' => 45, 'parent' => 41, 'title' => 'XXX7', 'children'=>array()), | |
array('id' => 19, 'parent' => 92, 'title' => 'XXX8', 'children'=>array()), | |
array('id' => 101, 'parent' => 45, 'title' => 'XXX9', 'children'=>array()), | |
array('id' => 102, 'parent' => 45, 'title' => 'XXX10', 'children'=>array()), | |
array('id' => 103, 'parent' => 19, 'title' => 'XXX11', 'children'=>array()), | |
array('id' => 104, 'parent' => 19, 'title' => 'XXX12', 'children'=>array()), | |
array('id' => 105, 'parent' => 19, 'title' => 'XXX13', 'children'=>array()) | |
); | |
$newArr = unflattenArray($arr); | |
echo "<pre>"; | |
print_r($newArr); | |
echo "</pre>"; | |
function unflattenArray($flatArray){ | |
$refs = array(); //for setting children without having to search the parents in the result tree. | |
$result = array(); | |
//process all elements until nohting could be resolved. | |
//then add remaining elements to the root one by one. | |
while(count($flatArray) > 0){ | |
for ($i=count($flatArray)-1; $i>=0; $i--){ | |
if ($flatArray[$i]["parent"]==0){ | |
//root element: set in result and ref! | |
$result[$flatArray[$i]["id"]] = $flatArray[$i]; | |
$refs[$flatArray[$i]["id"]] = &$result[$flatArray[$i]["id"]]; | |
unset($flatArray[$i]); | |
$flatArray = array_values($flatArray); | |
} | |
else if ($flatArray[$i]["parent"] != 0){ | |
//no root element. Push to the referenced parent, and add to references as well. | |
if (array_key_exists($flatArray[$i]["parent"], $refs)){ | |
//parent found | |
$o = $flatArray[$i]; | |
$refs[$flatArray[$i]["id"]] = $o; | |
$refs[$flatArray[$i]["parent"]]["children"][] = &$refs[$flatArray[$i]["id"]]; | |
unset($flatArray[$i]); | |
$flatArray = array_values($flatArray); | |
} | |
} | |
} | |
} | |
return $result; | |
} | |
/*sorting*/ | |
sortMyArrays($newArr); | |
echo "<pre>"; | |
print_r($newArr); | |
echo "</pre>"; | |
function sortMyArrays(&$arr){ | |
uasort($arr, "srt"); | |
foreach ($arr as $a) { | |
sortMyArrays($a["children"]); | |
} | |
} | |
function srt($a, $b){ | |
return $a["id"] - $b["id"]; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment