3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $arr = array ( 1 => array ( 'id' => '1', 'name' => ' Category', 'id_parent' => '0', 'ativo' => '1', ), 2 => array ( 'id' => '2', 'name' => ' Slippers', 'id_parent' => '0', 'ativo' => '1', ), 3 => array ( 'id' => '3', 'name' => ' TShirts', 'id_parent' => '0', 'ativo' => '1', ), 4 => array ( 'id' => '4', 'name' => ' BlousesSweatshirt', 'id_parent' => '0', 'ativo' => '1', ), 5 => array ( 'id' => '5', 'name' => ' Cap', 'id_parent' => '0', 'ativo' => '1', ), 6 => array ( 'id' => '6', 'name' => ' Stickers', 'id_parent' => '0', 'ativo' => '1', ), 7 => array ( 'id' => '7', 'name' => ' ScreensandFrames', 'id_parent' => '0', 'ativo' => '1', ), 8 => array ( 'id' => '8', 'name' => ' Models', 'id_parent' => '0', 'ativo' => '1', ), 9 => array ( 'id' => '9', 'name' => ' Notebook', 'id_parent' => '6', 'ativo' => '1', ), 10 => array ( 'id' => '10', 'name' => ' Door', 'id_parent' => '0', 'ativo' => '1', ), 11 => array ( 'id' => '11', 'name' => ' Door', 'id_parent' => '6', 'ativo' => '1', ), 12 => array ( 'id' => '12', 'name' => ' Kangaroo', 'id_parent' => '4', 'ativo' => '1', ), 13 => array ( 'id' => '13', 'name' => ' KangarooRaglan', 'id_parent' => '4', 'ativo' => '1', ), 14 => array ( 'id' => '14', 'name' => ' RoundCollar', 'id_parent' => '4', 'ativo' => '1', ), 15 => array ( 'id' => '15', 'name' => ' Trucker', 'id_parent' => '5', 'ativo' => '1', ), 16 => array ( 'id' => '16', 'name' => ' Basic', 'id_parent' => '3', 'ativo' => '1', ), 17 => array ( 'id' => '17', 'name' => ' Longline', 'id_parent' => '3', 'ativo' => '1', ), 18 => array ( 'id' => '18', 'name' => ' Raglan', 'id_parent' => '3', 'ativo' => '1', ), 19 => array ( 'id' => '19', 'name' => ' Raglan3/4', 'id_parent' => '3', 'ativo' => '1', ), 20 => array ( 'id' => '20', 'name' => ' Regatta', 'id_parent' => '3', 'ativo' => '1', ), 21 => array ( 'id' => '21', 'name' => ' Slide', 'id_parent' => '2', 'ativo' => '1', ), 22 => array ( 'id' => '22', 'name' => ' Stickers', 'id_parent' => '8', 'ativo' => '1', ), 23 => array ( 'id' => '23', 'name' => ' Notebook', 'id_parent' => '22', 'ativo' => '1', ), 24 => array ( 'id' => '24', 'name' => ' T-shirt', 'id_parent' => '8', 'ativo' => '1', ), 25 => array ( 'id' => '25', 'name' => ' Basic', 'id_parent' => '24', 'ativo' => '1', ), 26 => array ( 'id' => '26', 'name' => ' Slippers', 'id_parent' => '8', 'ativo' => '1', ), 27 => array ( 'id' => '27', 'name' => ' Slide', 'id_parent' => '26', 'ativo' => '1', ), 28 => array ( 'id' => '28', 'name' => ' 1Screen', 'id_parent' => '7', 'ativo' => '1', ), 29 => array ( 'id' => '29', 'name' => ' Set3Screens', 'id_parent' => '7', 'ativo' => '1', ), 30 => array ( 'id' => '30', 'name' => ' Set5Screens', 'id_parent' => '7', 'ativo' => '1', ), 31 => array ( 'id' => '31', 'name' => ' BlousesSweatshirt', 'id_parent' => '8', 'ativo' => '1', ), 32 => array ( 'id' => '32', 'name' => ' Cap', 'id_parent' => '8', 'ativo' => '1', ), 33 => array ( 'id' => '33', 'name' => ' ScreensandFrames', 'id_parent' => '8', 'ativo' => '1', ), ); $arr = array_merge([0 => ['id' => 0,'id_parent' => '0']], $arr); $new =[]; Foreach(array_reverse($arr) as $sub){ Echo $sub['id'] . "\n"; If(array_key_exists($sub['id_parent'], $new)){ $new[$sub['id_parent']][$sub['id']] = $sub; }Else{ $new[$sub['id_parent']] = $arr[$sub['id_parent']]; $new[$sub['id_parent']][$sub['id']] = $sub; } } //sort($new); Var_dump($new);
based on moaCr
Output for 5.6.30, hhvm-3.18.5 - 3.22.0, 7.0.30 - 7.3.0rc3
33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 array(11) { [8]=> array(10) { ["id"]=> string(1) "8" ["name"]=> string(7) " Models" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [33]=> array(4) { ["id"]=> string(2) "33" ["name"]=> string(17) " ScreensandFrames" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" } [32]=> array(4) { ["id"]=> string(2) "32" ["name"]=> string(4) " Cap" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" } [31]=> array(4) { ["id"]=> string(2) "31" ["name"]=> string(18) " BlousesSweatshirt" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" } [26]=> array(4) { ["id"]=> string(2) "26" ["name"]=> string(9) " Slippers" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" } [24]=> array(4) { ["id"]=> string(2) "24" ["name"]=> string(8) " T-shirt" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" } [22]=> array(4) { ["id"]=> string(2) "22" ["name"]=> string(9) " Stickers" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" } } [7]=> array(7) { ["id"]=> string(1) "7" ["name"]=> string(17) " ScreensandFrames" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [30]=> array(4) { ["id"]=> string(2) "30" ["name"]=> string(12) " Set5Screens" ["id_parent"]=> string(1) "7" ["ativo"]=> string(1) "1" } [29]=> array(4) { ["id"]=> string(2) "29" ["name"]=> string(12) " Set3Screens" ["id_parent"]=> string(1) "7" ["ativo"]=> string(1) "1" } [28]=> array(4) { ["id"]=> string(2) "28" ["name"]=> string(8) " 1Screen" ["id_parent"]=> string(1) "7" ["ativo"]=> string(1) "1" } } [26]=> array(5) { ["id"]=> string(2) "26" ["name"]=> string(9) " Slippers" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" [27]=> array(4) { ["id"]=> string(2) "27" ["name"]=> string(6) " Slide" ["id_parent"]=> string(2) "26" ["ativo"]=> string(1) "1" } } [24]=> array(5) { ["id"]=> string(2) "24" ["name"]=> string(8) " T-shirt" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" [25]=> array(4) { ["id"]=> string(2) "25" ["name"]=> string(6) " Basic" ["id_parent"]=> string(2) "24" ["ativo"]=> string(1) "1" } } [22]=> array(5) { ["id"]=> string(2) "22" ["name"]=> string(9) " Stickers" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" [23]=> array(4) { ["id"]=> string(2) "23" ["name"]=> string(9) " Notebook" ["id_parent"]=> string(2) "22" ["ativo"]=> string(1) "1" } } [2]=> array(5) { ["id"]=> string(1) "2" ["name"]=> string(9) " Slippers" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [21]=> array(4) { ["id"]=> string(2) "21" ["name"]=> string(6) " Slide" ["id_parent"]=> string(1) "2" ["ativo"]=> string(1) "1" } } [3]=> array(9) { ["id"]=> string(1) "3" ["name"]=> string(8) " TShirts" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [20]=> array(4) { ["id"]=> string(2) "20" ["name"]=> string(8) " Regatta" ["id_parent"]=> string(1) "3" ["ativo"]=> string(1) "1" } [19]=> array(4) { ["id"]=> string(2) "19" ["name"]=> string(10) " Raglan3/4" ["id_parent"]=> string(1) "3" ["ativo"]=> string(1) "1" } [18]=> array(4) { ["id"]=> string(2) "18" ["name"]=> string(7) " Raglan" ["id_parent"]=> string(1) "3" ["ativo"]=> string(1) "1" } [17]=> array(4) { ["id"]=> string(2) "17" ["name"]=> string(9) " Longline" ["id_parent"]=> string(1) "3" ["ativo"]=> string(1) "1" } [16]=> array(4) { ["id"]=> string(2) "16" ["name"]=> string(6) " Basic" ["id_parent"]=> string(1) "3" ["ativo"]=> string(1) "1" } } [5]=> array(5) { ["id"]=> string(1) "5" ["name"]=> string(4) " Cap" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [15]=> array(4) { ["id"]=> string(2) "15" ["name"]=> string(8) " Trucker" ["id_parent"]=> string(1) "5" ["ativo"]=> string(1) "1" } } [4]=> array(7) { ["id"]=> string(1) "4" ["name"]=> string(18) " BlousesSweatshirt" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [14]=> array(4) { ["id"]=> string(2) "14" ["name"]=> string(12) " RoundCollar" ["id_parent"]=> string(1) "4" ["ativo"]=> string(1) "1" } [13]=> array(4) { ["id"]=> string(2) "13" ["name"]=> string(15) " KangarooRaglan" ["id_parent"]=> string(1) "4" ["ativo"]=> string(1) "1" } [12]=> array(4) { ["id"]=> string(2) "12" ["name"]=> string(9) " Kangaroo" ["id_parent"]=> string(1) "4" ["ativo"]=> string(1) "1" } } [6]=> array(6) { ["id"]=> string(1) "6" ["name"]=> string(9) " Stickers" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [11]=> array(4) { ["id"]=> string(2) "11" ["name"]=> string(5) " Door" ["id_parent"]=> string(1) "6" ["ativo"]=> string(1) "1" } [9]=> array(4) { ["id"]=> string(1) "9" ["name"]=> string(9) " Notebook" ["id_parent"]=> string(1) "6" ["ativo"]=> string(1) "1" } } [0]=> array(12) { ["id"]=> int(0) ["id_parent"]=> string(1) "0" [10]=> array(4) { ["id"]=> string(2) "10" ["name"]=> string(5) " Door" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" } [8]=> array(4) { ["id"]=> string(1) "8" ["name"]=> string(7) " Models" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" } [7]=> array(4) { ["id"]=> string(1) "7" ["name"]=> string(17) " ScreensandFrames" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" } [6]=> array(4) { ["id"]=> string(1) "6" ["name"]=> string(9) " Stickers" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" } [5]=> array(4) { ["id"]=> string(1) "5" ["name"]=> string(4) " Cap" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" } [4]=> array(4) { ["id"]=> string(1) "4" ["name"]=> string(18) " BlousesSweatshirt" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" } [3]=> array(4) { ["id"]=> string(1) "3" ["name"]=> string(8) " TShirts" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" } [2]=> array(4) { ["id"]=> string(1) "2" ["name"]=> string(9) " Slippers" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" } [1]=> array(4) { ["id"]=> string(1) "1" ["name"]=> string(9) " Category" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" } [0]=> array(2) { ["id"]=> int(0) ["id_parent"]=> string(1) "0" } } }