3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data=array(); $all = array( array('id'=>1,'ParentID'=>'0','type'=>'Men'), array('id'=>2,'ParentID'=>'0','type'=>'Women'), array('id'=>3,'ParentID'=>'0','type'=>'Kids'), array('id'=>4,'ParentID'=>'1','type'=>'Shirt'), array('id'=>5,'ParentID'=>'2','type'=>'Top'), array('id'=>6,'ParentID'=>'3','type'=>'Shoes'), array('id'=>7,'ParentID'=>'4','type'=>'p1'), array('id'=>8,'ParentID'=>'5','type'=>'p1'), array('id'=>9,'ParentID'=>'6','type'=>'p1'), array('id'=>10,'ParentID'=>'4','type'=>'p2'), array('id'=>11,'ParentID'=>'5','type'=>'p2'), array('id'=>12,'ParentID'=>'6','type'=>'p2') ); foreach($all as $key => $val) { if($val['ParentID']==0) { $data[$key]=$val; foreach($all as $k => $v) { if($val['id'] == $v['ParentID']){ $data[$key]['subBranches'][$key]= $v; foreach($all as $a => $s) { if($v['id'] == $s['ParentID']){ $data[$key]['subBranches'][$key]['sub_subBranches'][]= $s; } } } } } } echo "<pre>"; print_r($data); echo json_encode($data); ?>
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.30, 7.0.0 - 7.0.24, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
<pre>Array ( [0] => Array ( [id] => 1 [ParentID] => 0 [type] => Men [subBranches] => Array ( [0] => Array ( [id] => 4 [ParentID] => 1 [type] => Shirt [sub_subBranches] => Array ( [0] => Array ( [id] => 7 [ParentID] => 4 [type] => p1 ) [1] => Array ( [id] => 10 [ParentID] => 4 [type] => p2 ) ) ) ) ) [1] => Array ( [id] => 2 [ParentID] => 0 [type] => Women [subBranches] => Array ( [1] => Array ( [id] => 5 [ParentID] => 2 [type] => Top [sub_subBranches] => Array ( [0] => Array ( [id] => 8 [ParentID] => 5 [type] => p1 ) [1] => Array ( [id] => 11 [ParentID] => 5 [type] => p2 ) ) ) ) ) [2] => Array ( [id] => 3 [ParentID] => 0 [type] => Kids [subBranches] => Array ( [2] => Array ( [id] => 6 [ParentID] => 3 [type] => Shoes [sub_subBranches] => Array ( [0] => Array ( [id] => 9 [ParentID] => 6 [type] => p1 ) [1] => Array ( [id] => 12 [ParentID] => 6 [type] => p2 ) ) ) ) ) ) [{"id":1,"ParentID":"0","type":"Men","subBranches":[{"id":4,"ParentID":"1","type":"Shirt","sub_subBranches":[{"id":7,"ParentID":"4","type":"p1"},{"id":10,"ParentID":"4","type":"p2"}]}]},{"id":2,"ParentID":"0","type":"Women","subBranches":{"1":{"id":5,"ParentID":"2","type":"Top","sub_subBranches":[{"id":8,"ParentID":"5","type":"p1"},{"id":11,"ParentID":"5","type":"p2"}]}}},{"id":3,"ParentID":"0","type":"Kids","subBranches":{"2":{"id":6,"ParentID":"3","type":"Shoes","sub_subBranches":[{"id":9,"ParentID":"6","type":"p1"},{"id":12,"ParentID":"6","type":"p2"}]}}}]
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 <pre>Array ( [0] => Array ( [id] => 1 [ParentID] => 0 [type] => Men [subBranches] => Array ( [0] => Array ( [id] => 4 [ParentID] => 1 [type] => Shirt [sub_subBranches] => Array ( [0] => Array ( [id] => 7 [ParentID] => 4 [type] => p1 ) [1] => Array ( [id] => 10 [ParentID] => 4 [type] => p2 ) ) ) ) ) [1] => Array ( [id] => 2 [ParentID] => 0 [type] => Women [subBranches] => Array ( [1] => Array ( [id] => 5 [ParentID] => 2 [type] => Top [sub_subBranches] => Array ( [0] => Array ( [id] => 8 [ParentID] => 5 [type] => p1 ) [1] => Array ( [id] => 11 [ParentID] => 5 [type] => p2 ) ) ) ) ) [2] => Array ( [id] => 3 [ParentID] => 0 [type] => Kids [subBranches] => Array ( [2] => Array ( [id] => 6 [ParentID] => 3 [type] => Shoes [sub_subBranches] => Array ( [0] => Array ( [id] => 9 [ParentID] => 6 [type] => p1 ) [1] => Array ( [id] => 12 [ParentID] => 6 [type] => p2 ) ) ) ) ) ) [{"id":1,"ParentID":"0","type":"Men","subBranches":[{"id":4,"ParentID":"1","type":"Shirt","sub_subBranches":[{"id":7,"ParentID":"4","type":"p1"},{"id":10,"ParentID":"4","type":"p2"}]}]},{"id":2,"ParentID":"0","type":"Women","subBranches":{"1":{"id":5,"ParentID":"2","type":"Top","sub_subBranches":[{"id":8,"ParentID":"5","type":"p1"},{"id":11,"ParentID":"5","type":"p2"}]}}},{"id":3,"ParentID":"0","type":"Kids","subBranches":{"2":{"id":6,"ParentID":"3","type":"Shoes","sub_subBranches":[{"id":9,"ParentID":"6","type":"p1"},{"id":12,"ParentID":"6","type":"p2"}]}}}]

preferences:
219.86 ms | 413 KiB | 247 Q