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; } //============================================ // Кейс 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($json)); //============================================ // Кейс 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($json2));

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.140.0030.00517.13
8.3.130.0030.00916.50
8.3.120.0070.00718.84
8.3.110.0090.00020.94
8.3.100.0030.00624.06
8.3.90.0000.00826.77
8.3.80.0060.00317.97
8.3.70.0060.00918.17
8.3.60.0120.00918.31
8.3.50.0090.00916.57
8.3.40.0100.00321.93
8.3.30.0110.00419.34
8.3.20.0040.00424.18
8.3.10.0070.00024.66
8.3.00.0040.00426.16
8.2.250.0060.00916.71
8.2.240.0080.00017.23
8.2.230.0040.00422.58
8.2.220.0130.00637.54
8.2.210.0110.00726.77
8.2.200.0030.00616.25
8.2.190.0090.00916.58
8.2.180.0070.00718.41
8.2.170.0080.00818.81
8.2.160.0070.00722.96
8.2.150.0000.00725.66
8.2.140.0080.00024.66
8.2.130.0040.00426.16
8.2.120.0070.00019.79
8.2.110.0050.00520.97
8.2.100.0040.00817.66
8.2.90.0050.00317.75
8.2.80.0040.00418.83
8.2.70.0050.00317.48
8.2.60.0120.00617.13
8.2.50.0140.00717.25
8.2.40.0100.00717.47
8.2.30.0120.00517.87
8.2.20.0080.00817.38
8.2.10.0160.00017.50
8.2.00.0160.00017.61
8.1.300.0060.00318.48
8.1.290.0030.00630.84
8.1.280.0060.00925.92
8.1.270.0030.00620.65
8.1.260.0080.00026.35
8.1.250.0080.00028.09
8.1.240.0150.00619.52
8.1.230.0110.00021.00
8.1.220.0030.00518.68
8.1.210.0040.00418.77
8.1.200.0050.00317.35
8.1.190.0170.00017.11
8.1.180.0130.00617.25
8.1.170.0140.00517.13
8.1.160.0100.01016.96
8.1.150.0190.00017.11
8.1.140.0140.00717.13
8.1.130.0110.00817.25
8.1.120.0100.00817.34
8.1.110.0130.00417.38
8.1.100.0160.00417.13
8.1.90.0040.01217.13
8.1.80.0090.00617.25
8.1.70.0080.00817.24
8.1.60.0070.01117.38
8.1.50.0110.00617.38
8.1.40.0130.00517.25
8.1.30.0120.00617.48
8.1.20.0060.01117.38
8.1.10.0160.00317.00
8.1.00.0120.00617.25
8.0.300.0060.00318.77
8.0.290.0050.00316.75
8.0.280.0120.00416.52
8.0.270.0170.00016.52
8.0.260.0080.00816.52
8.0.250.0160.00416.61
8.0.240.0170.00016.73
8.0.230.0100.00716.52
8.0.220.0140.00516.52
8.0.210.0170.00016.59
8.0.200.0100.00716.75
8.0.190.0150.00416.59
8.0.180.0100.00716.75
8.0.170.0110.00716.52
8.0.160.0070.01016.52
8.0.150.0120.00516.75
8.0.140.0080.00816.60
8.0.130.0130.00616.61
8.0.120.0110.00616.75
8.0.110.0130.00316.62
8.0.100.0190.00016.52
8.0.90.0100.00816.63
8.0.80.0130.00416.63
8.0.70.0150.00416.52
8.0.60.0180.00016.63
8.0.50.0140.00316.52
8.0.30.0110.00516.75
8.0.20.0150.00316.52
8.0.10.0080.00816.88

preferences:
50.73 ms | 403 KiB | 5 Q