3v4l.org

run code in 300+ PHP versions simultaneously
<?php $category = '{"9":{"id":"9","btc_mlm_user_id":"0","lft":"1","rht":"16","lvl":"0","name":"Root","created":"2017-06-27 05:56:11","modified":"2017-06-27 05:56:11","first_name":"","last_name":"","username":""},"42":{"id":"42","btc_mlm_user_id":"25","lft":"2","rht":"13","lvl":"1","name":"naresh","created":"2017-11-02 10:22:24","modified":"2017-11-02 10:22:24","first_name":"","last_name":"","username":"naresh"},"44":{"id":"44","btc_mlm_user_id":"27","lft":"3","rht":"4","lvl":"2","name":"rahul1","created":"2017-11-02 10:25:53","modified":"2017-11-02 10:25:53","first_name":"","last_name":"","username":"rahul1"},"45":{"id":"45","btc_mlm_user_id":"28","lft":"5","rht":"6","lvl":"2","name":"rahul123","created":"2017-11-02 10:27:19","modified":"2017-11-02 10:27:19","first_name":"","last_name":"","username":"rahul123"},"46":{"id":"46","btc_mlm_user_id":"29","lft":"7","rht":"12","lvl":"2","name":"kapil1","created":"2017-11-02 10:28:20","modified":"2017-11-02 10:28:20","first_name":"","last_name":"","username":"kapil1"},"47":{"id":"47","btc_mlm_user_id":"30","lft":"8","rht":"11","lvl":"3","name":"priya12","created":"2017-11-02 10:30:30","modified":"2017-11-02 10:30:30","first_name":"","last_name":"","username":"priya12"},"48":{"id":"48","btc_mlm_user_id":"31","lft":"9","rht":"10","lvl":"4","name":"amit12","created":"2017-11-02 10:32:00","modified":"2017-11-02 10:32:00","first_name":"","last_name":"","username":"amit12"},"43":{"id":"43","btc_mlm_user_id":"26","lft":"14","rht":"15","lvl":"1","name":"roshan","created":"2017-11-02 10:24:27","modified":"2017-11-02 10:24:27","first_name":"","last_name":"","username":"roshan"}}'; $t = []; function tree($data, $left = 0, $right = null) { $tree = array(); foreach ($data as $key => $value) { if ($value['lft'] == $left + 1 && (is_null($right) || $value['rht'] < $right)) { $child = []; $child['text'] = ['name' => $value['name']]; $childTree = tree($data, $value['lft'], $value['rht']); if (!empty($childTree)) { $child['children'] = $childTree; } $tree[] = $child; $left = $value['rht']; } } return $tree; } $tree = tree(json_decode($category, true))[0]; print_r($tree); echo json_encode($tree);
Output for 7.0.0 - 7.0.25, 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
Array ( [text] => Array ( [name] => Root ) [children] => Array ( [0] => Array ( [text] => Array ( [name] => naresh ) [children] => Array ( [0] => Array ( [text] => Array ( [name] => rahul1 ) ) [1] => Array ( [text] => Array ( [name] => rahul123 ) ) [2] => Array ( [text] => Array ( [name] => kapil1 ) [children] => Array ( [0] => Array ( [text] => Array ( [name] => priya12 ) [children] => Array ( [0] => Array ( [text] => Array ( [name] => amit12 ) ) ) ) ) ) ) ) [1] => Array ( [text] => Array ( [name] => roshan ) ) ) ) {"text":{"name":"Root"},"children":[{"text":{"name":"naresh"},"children":[{"text":{"name":"rahul1"}},{"text":{"name":"rahul123"}},{"text":{"name":"kapil1"},"children":[{"text":{"name":"priya12"},"children":[{"text":{"name":"amit12"}}]}]}]},{"text":{"name":"roshan"}}]}
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 Array ( [text] => Array ( [name] => Root ) [children] => Array ( [0] => Array ( [text] => Array ( [name] => naresh ) [children] => Array ( [0] => Array ( [text] => Array ( [name] => rahul1 ) ) [1] => Array ( [text] => Array ( [name] => rahul123 ) ) [2] => Array ( [text] => Array ( [name] => kapil1 ) [children] => Array ( [0] => Array ( [text] => Array ( [name] => priya12 ) [children] => Array ( [0] => Array ( [text] => Array ( [name] => amit12 ) ) ) ) ) ) ) ) [1] => Array ( [text] => Array ( [name] => roshan ) ) ) ) {"text":{"name":"Root"},"children":[{"text":{"name":"naresh"},"children":[{"text":{"name":"rahul1"}},{"text":{"name":"rahul123"}},{"text":{"name":"kapil1"},"children":[{"text":{"name":"priya12"},"children":[{"text":{"name":"amit12"}}]}]}]},{"text":{"name":"roshan"}}]}

preferences:
205.43 ms | 413 KiB | 181 Q