3v4l.org

run code in 300+ PHP versions simultaneously
<?php $orderList = [ 'Jake', 'Scully' => [ 'pet', 'friend' => [ 'Michael', 'Merissa', ], 'Norm', ], 'Amy', 'Charles', 'Hitchcock', ]; $items = [ [ 'name' => 'Scully', 'related' => [ [ 'name' => 'Norm', 'category' => 'self' ], [ 'name' => 'Earl', 'category' => 'friend' ], [ 'name' => 'Kelly', 'category' => 'pet' ], [ 'name' => 'Michael', 'category' => 'friend' ], [ 'name' => 'Merissa', 'category' => 'friend' ], ], ], [ 'name' => 'Rosa', 'related' => [], ], [ 'name' => 'Jake', 'related' => [ [ 'name' => 'Franzia', 'category' => 'nemesis' ], [ 'name' => 'Doug' ], ], ], [ 'name' => 'Hitchcock', 'related' => [], ], [ 'name' => 'Amy', 'related' => [], ], ]; function getTopLevelItemsFromMixedList(array $mixedList): array { $topLevels = []; foreach ($mixedList as $item => $value) { $topLevels[] = is_int($item) ? $value : $item; } return $topLevels; } function sort_by(array $array, callable $sortKeyResolver, ...$sortArgs) { $mapped = array_map($sortKeyResolver, $array); asort($mapped, ...$sortArgs); $sorted = []; foreach ($mapped as $key => $index) { $sorted[] = $array[$key]; } return $sorted; } $topLevelOrder = getTopLevelItemsFromMixedList($orderList); $sortKeyResolver = function ($item) use ($topLevelOrder) { $orderListIndex = array_search($item['name'], $topLevelOrder); return $orderListIndex === false ? count($topLevelOrder).$item['name'] : $orderListIndex; }; // First, sort parents $items = sort_by($items, $sortKeyResolver, SORT_NATURAL); // Then sort children foreach ($items as &$item) { $children = $item['related'] ?? []; if(empty($orderList[$item['name']])) { // No order was specified for the children, so just sort them as normal $item['related'] = sort_by($children, fn ($child) => $child['name'], SORT_NATURAL); continue; } // An order was specified; it can contain children names or categories, // so let'sjust call it the "Level 2" order (L2). $l2Order = getTopLevelItemsFromMixedList($orderList[$item['name']]); $sortKeyResolver = function ($childItem) use ($l2Order) { // First, we check if there's an entry for the item's name $indexOfChildInL2Order = array_search($childItem['name'], $l2Order); if ($indexOfChildInL2Order !== false) { return $indexOfChildInL2Order; } // Check if there's an entry for the item's category $indexOfCategoryInL2Order = array_search($childItem['category'], $l2Order); if ($indexOfCategoryInL2Order !== false) { // There's an entry for the category! // Now check if there's an entry for the item within the category. $orderOfChildrenInCategory = $l2Order[$childItem['category']] ?? []; $indexOfChildInCategory = array_search($childItem['name'], $orderOfChildrenInCategory); return $indexOfChildInCategory === false ? $indexOfCategoryInL2Order.$childItem['name'] : ($indexOfCategoryInL2Order + ($indexOfChildInCategory * 0.1)); } return count($l2Order).$childItem['name']; }; $item['related'] = sort_by($children, $sortKeyResolver, SORT_NATURAL); } print_r($items);

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.4.20.0160.00319.75
8.4.10.0060.00322.25
8.3.150.0150.00320.82
8.3.140.0130.00620.77
8.3.130.0090.00018.70
8.3.120.0040.00819.20
8.3.110.0030.00616.89
8.3.100.0080.00018.41
8.3.90.0040.00426.77
8.3.80.0080.00316.50
8.3.70.0080.00816.63
8.3.60.0090.00616.88
8.3.50.0070.01018.20
8.3.40.0110.00420.29
8.3.30.0070.00718.63
8.3.20.0050.00224.18
8.3.10.0000.00924.66
8.3.00.0080.00026.16
8.2.260.0000.00717.21
8.2.250.0000.00918.48
8.2.240.0150.00417.55
8.2.230.0130.01020.94
8.2.220.0150.00924.06
8.2.210.0030.01726.77
8.2.200.0030.00616.50
8.2.190.0070.01416.88
8.2.180.0100.00725.92
8.2.170.0030.01722.96
8.2.160.0100.01022.96
8.2.150.0000.00925.66
8.2.140.0040.00424.66
8.2.130.0080.00026.16
8.2.120.0040.00419.66
8.2.110.0070.00320.39
8.2.100.0090.00020.39
8.1.310.0070.00016.83
8.1.300.0090.00017.66
8.1.290.0090.00030.84
8.1.280.0110.00425.92
8.1.270.0080.00024.66
8.1.260.0090.00026.35
8.1.250.0080.00028.09
8.1.240.0070.01118.52
8.1.230.0070.00318.81
8.1.100.0050.00417.48

preferences:
26.33 ms | 403 KiB | 5 Q