3v4l.org

run code in 300+ PHP versions simultaneously
<?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', ), ); $new =[]; Foreach(array_reverse($arr) as $sub){ If(array_key_exists($new[$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; } } sort($new); Var_dump($new);
Output for 8.3.0 - 8.3.4, 8.3.6
Warning: Undefined array key 8 in /in/YRNWf on line 239 Fatal error: Uncaught TypeError: Cannot access offset of type array on array in /in/YRNWf:239 Stack trace: #0 {main} thrown in /in/YRNWf on line 239
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Warning: Undefined array key 8 in /in/YRNWf on line 239 Fatal error: Uncaught TypeError: Cannot access offset of type array on array in /in/YRNWf:239 Stack trace: #0 {main} thrown in /in/YRNWf on line 239
Process exited with code 255.
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18
Warning: Undefined array key 8 in /in/YRNWf on line 239 Fatal error: Uncaught TypeError: Illegal offset type in /in/YRNWf:239 Stack trace: #0 {main} thrown in /in/YRNWf on line 239
Process exited with code 255.
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined offset: 8 in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Notice: Undefined offset: 7 in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Notice: Undefined offset: 26 in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Notice: Undefined offset: 24 in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Notice: Undefined offset: 22 in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Notice: Undefined offset: 2 in /in/YRNWf on line 239 Notice: Undefined offset: 3 in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Notice: Undefined offset: 5 in /in/YRNWf on line 239 Notice: Undefined offset: 4 in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Notice: Undefined offset: 6 in /in/YRNWf on line 239 Notice: Undefined offset: 0 in /in/YRNWf on line 239 Notice: Undefined offset: 0 in /in/YRNWf on line 242 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Notice: Undefined offset: 0 in /in/YRNWf on line 242 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Notice: Undefined offset: 0 in /in/YRNWf on line 242 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Notice: Undefined offset: 0 in /in/YRNWf on line 242 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Notice: Undefined offset: 0 in /in/YRNWf on line 242 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Notice: Undefined offset: 0 in /in/YRNWf on line 242 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Notice: Undefined offset: 0 in /in/YRNWf on line 242 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Notice: Undefined offset: 0 in /in/YRNWf on line 242 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Notice: Undefined offset: 0 in /in/YRNWf on line 242 array(11) { [0]=> array(1) { [0]=> array(4) { ["id"]=> string(1) "1" ["name"]=> string(9) " Category" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" } } [1]=> array(5) { ["id"]=> string(1) "2" ["name"]=> string(9) " Slippers" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "21" ["name"]=> string(6) " Slide" ["id_parent"]=> string(1) "2" ["ativo"]=> string(1) "1" } } [2]=> array(5) { ["id"]=> string(1) "3" ["name"]=> string(8) " TShirts" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "16" ["name"]=> string(6) " Basic" ["id_parent"]=> string(1) "3" ["ativo"]=> string(1) "1" } } [3]=> array(5) { ["id"]=> string(1) "4" ["name"]=> string(18) " BlousesSweatshirt" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "12" ["name"]=> string(9) " Kangaroo" ["id_parent"]=> string(1) "4" ["ativo"]=> string(1) "1" } } [4]=> array(5) { ["id"]=> string(1) "5" ["name"]=> string(4) " Cap" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "15" ["name"]=> string(8) " Trucker" ["id_parent"]=> string(1) "5" ["ativo"]=> string(1) "1" } } [5]=> array(5) { ["id"]=> string(1) "6" ["name"]=> string(9) " Stickers" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(1) "9" ["name"]=> string(9) " Notebook" ["id_parent"]=> string(1) "6" ["ativo"]=> string(1) "1" } } [6]=> array(5) { ["id"]=> string(1) "7" ["name"]=> string(17) " ScreensandFrames" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "28" ["name"]=> string(8) " 1Screen" ["id_parent"]=> string(1) "7" ["ativo"]=> string(1) "1" } } [7]=> array(5) { ["id"]=> string(1) "8" ["name"]=> string(7) " Models" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "22" ["name"]=> string(9) " Stickers" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" } } [8]=> array(5) { ["id"]=> string(2) "22" ["name"]=> string(9) " Stickers" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "23" ["name"]=> string(9) " Notebook" ["id_parent"]=> string(2) "22" ["ativo"]=> string(1) "1" } } [9]=> array(5) { ["id"]=> string(2) "24" ["name"]=> string(8) " T-shirt" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "25" ["name"]=> string(6) " Basic" ["id_parent"]=> string(2) "24" ["ativo"]=> string(1) "1" } } [10]=> array(5) { ["id"]=> string(2) "26" ["name"]=> string(9) " Slippers" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "27" ["name"]=> string(6) " Slide" ["id_parent"]=> string(2) "26" ["ativo"]=> string(1) "1" } } }
Output for 7.3.32 - 7.3.33
Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 Warning: array_key_exists(): The first argument should be either a string or an integer in /in/YRNWf on line 239 array(11) { [0]=> array(1) { [0]=> array(4) { ["id"]=> string(1) "1" ["name"]=> string(9) " Category" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" } } [1]=> array(5) { ["id"]=> string(1) "2" ["name"]=> string(9) " Slippers" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "21" ["name"]=> string(6) " Slide" ["id_parent"]=> string(1) "2" ["ativo"]=> string(1) "1" } } [2]=> array(5) { ["id"]=> string(1) "3" ["name"]=> string(8) " TShirts" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "16" ["name"]=> string(6) " Basic" ["id_parent"]=> string(1) "3" ["ativo"]=> string(1) "1" } } [3]=> array(5) { ["id"]=> string(1) "4" ["name"]=> string(18) " BlousesSweatshirt" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "12" ["name"]=> string(9) " Kangaroo" ["id_parent"]=> string(1) "4" ["ativo"]=> string(1) "1" } } [4]=> array(5) { ["id"]=> string(1) "5" ["name"]=> string(4) " Cap" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "15" ["name"]=> string(8) " Trucker" ["id_parent"]=> string(1) "5" ["ativo"]=> string(1) "1" } } [5]=> array(5) { ["id"]=> string(1) "6" ["name"]=> string(9) " Stickers" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(1) "9" ["name"]=> string(9) " Notebook" ["id_parent"]=> string(1) "6" ["ativo"]=> string(1) "1" } } [6]=> array(5) { ["id"]=> string(1) "7" ["name"]=> string(17) " ScreensandFrames" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "28" ["name"]=> string(8) " 1Screen" ["id_parent"]=> string(1) "7" ["ativo"]=> string(1) "1" } } [7]=> array(5) { ["id"]=> string(1) "8" ["name"]=> string(7) " Models" ["id_parent"]=> string(1) "0" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "22" ["name"]=> string(9) " Stickers" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" } } [8]=> array(5) { ["id"]=> string(2) "22" ["name"]=> string(9) " Stickers" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "23" ["name"]=> string(9) " Notebook" ["id_parent"]=> string(2) "22" ["ativo"]=> string(1) "1" } } [9]=> array(5) { ["id"]=> string(2) "24" ["name"]=> string(8) " T-shirt" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "25" ["name"]=> string(6) " Basic" ["id_parent"]=> string(2) "24" ["ativo"]=> string(1) "1" } } [10]=> array(5) { ["id"]=> string(2) "26" ["name"]=> string(9) " Slippers" ["id_parent"]=> string(1) "8" ["ativo"]=> string(1) "1" [0]=> array(4) { ["id"]=> string(2) "27" ["name"]=> string(6) " Slide" ["id_parent"]=> string(2) "26" ["ativo"]=> string(1) "1" } } }

preferences:
239.16 ms | 417 KiB | 287 Q