3v4l.org

run code in 300+ PHP versions simultaneously
<?php $itemList = array( array("id" => 100,"categorypath" => "level1/powders", "product" => "Mens powder"), array("id" => 200,"categorypath" => "level1/oils/sunflower", "product" => "XYZ oil"), array("id" => 300,"categorypath" => "level1/eatable/vegetables", "product" => "carrot"), array("id" => 400,"categorypath" => "level1/oils/sunflower", "product" => "ABC oil"), array("id" => 500,"categorypath" => "level1/eatable/fruits", "product" => "mango"), array("id" => 600,"categorypath" => "level1/eatable/vegetables", "product" => "spinach"), array("id" => 700,"categorypath" => "level2/baby items/toys", "product" => "puzzle block"), array("id" => 800,"categorypath" => "level2/baby items/toys", "product" => "trucks and cars"), array("id" => 900,"categorypath" => "level2/baby items/clothes", "product" => "shirts"), array("id" => 1000,"categorypath" => "level1/powders", "product" => "Womens powder"), array("id" => 1100,"categorypath" => "level1/oils/groundnut", "product" => "GN oil"), ); // First, group products into their categories. $categoryList = []; foreach ($itemList as $item){ $categoryList[$item['categorypath']][] = [ 'id' => $item['id'] , 'path' => $item['categorypath'] , 'prodname' => $item['product'] ]; } // Now, create a tree structure out of the categories $jsonTree = ['category'=>[]]; $lookupTable = []; foreach ($categoryList as $category=>$productList){ $componentList = explode('/', $category); $path = ''; $parent = &$jsonTree; do { $component = array_shift($componentList); $path .= $component.'/'; $entry = &$lookupTable[$path]; if (!$entry){ $entry = [ 'categoryName' => $component ]; $parent['category'][] = &$entry; } $parent = &$entry; } while ($componentList); $entry['products'] = $productList; } echo json_encode($jsonTree, JSON_PRETTY_PRINT);

Abusive script

This script was stopped while abusing our resources

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.110.0060.00320.94
8.3.100.0120.00316.34
8.3.90.0060.00326.77
8.3.80.0090.00619.36
8.3.70.0040.01116.58
8.3.60.0110.00718.55
8.3.50.0070.01623.78
8.3.40.0000.01521.98
8.3.30.0120.00318.70
8.3.20.0040.00424.18
8.3.10.0040.00424.66
8.3.00.0050.00226.16
8.2.230.0030.00622.58
8.2.220.0050.00524.06
8.2.210.0080.00026.77
8.2.200.0060.00318.88
8.2.190.0110.00416.58
8.2.180.0090.00625.92
8.2.170.0120.00318.83
8.2.160.0040.01422.96
8.2.150.0050.00325.66
8.2.140.0070.00024.66
8.2.130.0040.00426.16
8.2.120.0050.00326.35
8.2.110.0070.00320.97
8.2.100.0070.00417.84
8.2.90.0040.00419.89
8.2.80.0080.00020.54
8.2.70.0000.01218.05
8.2.60.0040.00418.16
8.2.50.0060.00318.10
8.2.40.0000.00819.32
8.2.30.0000.00719.27
8.2.20.0070.00018.07
8.2.10.0080.00019.22
8.2.00.0000.00819.36
8.1.290.0060.00330.84
8.1.280.0030.01225.92
8.1.270.0000.00823.99
8.1.260.0080.00028.09
8.1.250.0080.00028.09
8.1.240.0130.00722.05
8.1.230.0060.00621.03
8.1.220.0000.00817.74
8.1.210.0000.00818.98
8.1.200.0030.00617.48
8.1.190.0060.00317.22
8.1.180.0060.00318.10
8.1.170.0060.00318.68
8.1.160.0080.00019.06
8.1.150.0040.00418.64
8.1.140.0040.00418.88
8.1.130.0000.00720.05
8.1.120.0040.00317.49
8.1.110.0030.00617.41
8.1.100.0070.00317.38
8.1.90.0000.00717.44
8.1.80.0030.00717.41
8.1.70.0090.00017.49
8.1.60.0040.00417.46
8.1.50.0000.00817.53
8.1.40.0000.00817.56
8.1.30.0000.00917.69
8.1.20.0000.00817.48
8.1.10.0050.00317.56
8.1.00.0040.00417.50
8.0.300.0040.00420.21
8.0.290.0000.00816.63
8.0.280.0040.00418.31
8.0.270.0000.00717.93
8.0.260.0030.00318.45
8.0.250.0030.00316.86
8.0.240.0030.00316.92
8.0.230.0000.00716.88
8.0.220.0070.00016.93
8.0.210.0000.00716.94
8.0.200.0070.00016.93
8.0.190.0000.00716.81
8.0.180.0000.00716.80
8.0.170.0030.00616.90
8.0.160.0040.00416.91
8.0.150.0080.00016.85
8.0.140.0000.00716.74
8.0.130.0030.00313.32
8.0.120.0000.00716.75
8.0.110.0030.00516.91
8.0.100.0040.00416.86
8.0.90.0000.00716.80
8.0.80.0080.00816.91
8.0.70.0030.00616.84
8.0.60.0040.00416.80
8.0.50.0040.00416.91
8.0.30.0000.01717.13
8.0.20.0140.00817.26
8.0.10.0070.00016.88
8.0.00.0090.01216.76
7.4.330.0000.00515.55
7.4.320.0060.00016.59
7.4.300.0030.00316.55
7.4.290.0000.00716.61
7.4.280.0030.00716.63
7.4.270.0030.00316.63
7.4.260.0060.00013.31
7.4.250.0000.00816.55
7.4.240.0030.00516.50
7.4.230.0030.00316.57
7.4.220.0020.00516.59
7.4.210.0090.00616.63
7.4.200.0000.00716.66
7.4.130.0110.00616.36
7.4.120.0100.01316.56
7.4.110.0100.00716.52
7.4.100.0100.00716.50
7.4.90.0110.01116.46
7.4.80.0090.01216.48
7.4.70.0130.01016.46
7.4.60.0120.01216.28
7.4.50.0110.01116.58
7.4.40.0150.01216.49
7.4.30.0100.01416.55
7.4.20.0110.01116.62
7.4.10.0170.00916.18
7.4.00.0140.00716.50
7.3.330.0040.00216.16
7.3.320.0030.00313.16
7.3.310.0070.00016.46
7.3.300.0000.00716.17
7.3.290.0030.01216.37
7.3.260.0140.01116.45
7.3.230.0110.01216.42
7.3.220.0150.00816.33
7.3.210.0130.00716.33
7.3.200.0180.00616.29
7.3.190.0110.01116.24
7.3.180.0130.00616.05
7.3.170.0160.00916.46
7.3.160.0190.00516.46
7.3.150.0080.01216.27
7.3.140.0190.00516.55
7.3.130.0160.00516.60
7.3.120.0140.01116.50
7.3.110.0000.01916.29
7.3.100.0140.00616.39
7.3.90.0080.01016.43
7.3.80.0110.01016.45
7.3.70.0130.01016.33
7.3.60.0080.01316.36
7.3.50.0160.00616.38
7.3.40.0140.00716.39
7.3.30.0070.01016.36
7.3.20.0110.01116.51
7.3.10.0130.00616.42
7.3.00.0090.00916.31
7.2.330.0180.00316.89
7.2.320.0130.01316.43
7.2.310.0090.01216.73
7.2.300.0180.00716.56
7.2.290.0150.00516.57
7.2.280.0090.01216.59
7.2.270.0030.01616.80
7.2.260.0040.01916.38
7.2.250.0100.01016.68
7.2.240.0160.00316.78
7.2.230.0140.00716.80
7.2.220.0130.00816.80
7.2.210.0170.00716.97
7.2.200.0090.01216.65
7.2.190.0070.01416.70
7.2.180.0110.01416.69
7.2.170.0160.00316.70
7.2.160.0060.01216.63
7.2.150.0120.00616.92
7.2.140.0120.00616.68
7.2.130.0070.01016.58
7.2.120.0110.00716.64
7.2.110.0090.01516.71
7.2.100.0230.00016.72
7.2.90.0060.01716.71
7.2.80.0160.00416.75
7.2.70.0190.00616.62
7.2.60.0040.01516.61
7.2.50.0080.01216.88
7.2.40.0120.00616.75
7.2.30.0090.01616.73
7.2.20.0160.00816.69
7.2.10.0180.00716.54
7.2.00.0060.01416.66

preferences:
154.51 ms | 403 KiB | 5 Q