3v4l.org

run code in 300+ PHP versions simultaneously
<?php // https://axenov.dev/php-trees-without-recursion/ // функция построения дерева без рекурсии function tree( array $flat_list, string $fk = 'hid', string $pk = 'id', ): array { $tree = []; foreach ($flat_list as &$node) { $references[$node[$pk]] = &$node; if (empty($node[$fk])) { $tree[$node[$pk]] = &$node; } else { $references[$node[$fk]]['children'][] = &$node; } } return $tree; } // функция для преобразования дерева в плоский список function flatten( array $tree, string $branching = 'children' ): array { $result = []; while (count($tree)) { $node = array_shift($tree); if (!empty($node[$branching])) { $tree = array_merge($tree, $node[$branching]); unset($node[$branching]); } $result[] = $node; } return $result; } //============================================ // Кейс 1. Допустим, массив неассоциирован //============================================ // пример вспомогательной фукции для ассоциации массива function key_by_field(array $flat_list, string $pk = 'id'): array { $result = []; foreach ($flat_list as $element) { $result[$element[$pk]] = $element; } return $result; } // исходные данные $data = <<<JSON [ { "id": 1, "hid": null, "title": "Root" }, { "id": 2, "hid": 1, "title": "Branch 1" }, { "id": 3, "hid": 2, "title": "Leaf 1.1" }, { "id": 5, "hid": 1, "title": "Branch 2" }, { "id": 6, "hid": 5, "title": "Leaf 2.1" }, { "id": 4, "hid": 2, "title": "Leaf 1.2" }, { "id": 7, "hid": 5, "title": "Leaf 2.2" } ] JSON; $json = json_decode($data, JSON_OBJECT_AS_ARRAY | JSON_THROW_ON_ERROR); $json = key_by_field($json); // формирование нового списка элементов print_r($tree = tree($json)); // обратное преобразование в плоский список print_r(flatten($tree)); //============================================ // Кейс 2. Допустим, массив ассоциирован //============================================ $data2 = <<<JSON { "1": { "id": 1, "hid": null, "title": "Root" }, "3": { "id": 2, "hid": 2, "title": "Leaf 1.1" }, "2": { "id": 3, "hid": 1, "title": "Branch 1" }, "5": { "id": 5, "hid": 1, "title": "Branch 2" }, "6": { "id": 6, "hid": 5, "title": "Leaf 2.1" }, "4": { "id": 4, "hid": 2, "title": "Leaf 1.2" }, "7": { "id": 7, "hid": 5, "title": "Leaf 2.2" } } JSON; $json2 = json_decode($data2, JSON_OBJECT_AS_ARRAY | JSON_THROW_ON_ERROR); // формирование нового списка элементов print_r($tree = tree($json)); // обратное преобразование в плоский список print_r(flatten($tree));

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.30.0090.00620.16
8.3.20.0000.00824.18
8.3.10.0080.00024.66
8.3.00.0040.00426.16
8.2.160.0100.00322.96
8.2.150.0040.00425.66
8.2.140.0040.00424.66
8.2.130.0040.00426.16
8.2.120.0080.00022.13
8.2.110.0070.00322.08
8.2.100.0100.00317.79
8.2.90.0050.00318.34
8.2.80.0030.00519.11
8.2.70.0140.00479.33
8.2.60.0120.00779.33
8.2.50.0090.00979.33
8.2.40.0110.00679.33
8.2.30.0170.00279.33
8.2.20.0190.00079.33
8.2.10.0180.00479.33
8.2.00.0060.00979.33
8.1.270.0040.00424.66
8.1.260.0080.00026.35
8.1.250.0070.00028.09
8.1.240.0040.00423.88
8.1.230.0130.00520.89
8.1.220.0000.00817.74
8.1.210.0000.00818.77
8.1.200.0130.00379.33
8.1.190.0130.00479.33
8.1.180.0110.00779.33
8.1.170.0170.00379.33
8.1.160.0110.00879.33
8.1.150.0140.00579.33
8.1.140.0180.00079.33
8.1.130.0140.00479.33
8.1.120.0140.00379.33
8.1.110.0180.00079.33
8.1.100.0120.00579.33
8.1.90.0170.00079.33
8.1.80.0050.01179.33
8.1.70.0160.00079.33
8.1.60.0150.00379.33
8.1.50.0110.00779.33
8.1.40.0090.00979.33
8.1.30.0160.00379.33
8.1.20.0110.01179.33
8.1.10.0170.00379.33
8.1.00.0170.00479.33
8.0.300.0070.00020.21
8.0.290.0120.00479.33
8.0.280.0150.00379.33
8.0.270.0110.00879.33
8.0.260.0170.00079.33
8.0.250.0160.00079.33
8.0.240.0170.00379.33
8.0.230.0170.00079.33
8.0.220.0090.00779.33
8.0.210.0100.00779.33
8.0.200.0080.00879.33
8.0.190.0170.00079.33
8.0.180.0110.00679.33
8.0.170.0000.01779.33
8.0.160.0110.00679.33
8.0.150.0180.00079.33
8.0.140.0170.00079.33
8.0.130.0110.00579.33
8.0.120.0090.00679.33
8.0.110.0140.00379.33
8.0.100.0120.00479.33
8.0.90.0110.00679.33
8.0.80.0130.00679.33
8.0.70.0150.00479.33
8.0.60.0050.01479.33
8.0.50.0120.00679.33
8.0.30.0130.00379.33
8.0.20.0180.00079.33
8.0.10.0090.00679.33

preferences:
28.23 ms | 401 KiB | 5 Q