<?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)) {
$tree[$key]['text'] = ['name' => $value['name']];
$tree[$key]['children'] = tree($data, $value['lft'], $value['rht']);
$left = $value['rht'];
}
}
return $tree;
}
$tree = tree(json_decode($category, true));
print_r($tree);
echo json_encode($tree);
- Output for 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 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
(
[9] => Array
(
[text] => Array
(
[name] => Root
)
[children] => Array
(
[42] => Array
(
[text] => Array
(
[name] => naresh
)
[children] => Array
(
[44] => Array
(
[text] => Array
(
[name] => rahul1
)
[children] => Array
(
)
)
[45] => Array
(
[text] => Array
(
[name] => rahul123
)
[children] => Array
(
)
)
[46] => Array
(
[text] => Array
(
[name] => kapil1
)
[children] => Array
(
[47] => Array
(
[text] => Array
(
[name] => priya12
)
[children] => Array
(
[48] => Array
(
[text] => Array
(
[name] => amit12
)
[children] => Array
(
)
)
)
)
)
)
)
)
[43] => Array
(
[text] => Array
(
[name] => roshan
)
[children] => Array
(
)
)
)
)
)
{"9":{"text":{"name":"Root"},"children":{"42":{"text":{"name":"naresh"},"children":{"44":{"text":{"name":"rahul1"},"children":[]},"45":{"text":{"name":"rahul123"},"children":[]},"46":{"text":{"name":"kapil1"},"children":{"47":{"text":{"name":"priya12"},"children":{"48":{"text":{"name":"amit12"},"children":[]}}}}}}},"43":{"text":{"name":"roshan"},"children":[]}}}}
- 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
(
[9] => Array
(
[text] => Array
(
[name] => Root
)
[children] => Array
(
[42] => Array
(
[text] => Array
(
[name] => naresh
)
[children] => Array
(
[44] => Array
(
[text] => Array
(
[name] => rahul1
)
[children] => Array
(
)
)
[45] => Array
(
[text] => Array
(
[name] => rahul123
)
[children] => Array
(
)
)
[46] => Array
(
[text] => Array
(
[name] => kapil1
)
[children] => Array
(
[47] => Array
(
[text] => Array
(
[name] => priya12
)
[children] => Array
(
[48] => Array
(
[text] => Array
(
[name] => amit12
)
[children] => Array
(
)
)
)
)
)
)
)
)
[43] => Array
(
[text] => Array
(
[name] => roshan
)
[children] => Array
(
)
)
)
)
)
{"9":{"text":{"name":"Root"},"children":{"42":{"text":{"name":"naresh"},"children":{"44":{"text":{"name":"rahul1"},"children":[]},"45":{"text":{"name":"rahul123"},"children":[]},"46":{"text":{"name":"kapil1"},"children":{"47":{"text":{"name":"priya12"},"children":{"48":{"text":{"name":"amit12"},"children":[]}}}}}}},"43":{"text":{"name":"roshan"},"children":[]}}}}
preferences:
205.81 ms | 413 KiB | 217 Q