3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Here's the data: $json = json_decode('[ [{"id":1,"price":11900},{"id":2,"price":499},{"id":3,"price":2099}], [{"id":1,"price":11900},{"id":2,"price":499},{"id":4,"price":999}], [{"id":1,"price":11900},{"id":2,"price":499},{"id":5,"price":899}], [{"id":1,"price":11900},{"id":2,"price":499},{"id":6,"price":2999}] ]', true); // Calculate the sums for all prices up-front: $sums = array_map(fn($v) => array_sum(array_column($v, 'price')), $json); // Pass the sums into our key-based sorter: uksort($json, function($b, $a) use ($sums) { return $sums[$a] <=> $sums[$b]; }); // See the sums, get the sorted data: var_dump($sums, $json);

preferences:
31.81 ms | 410 KiB | 5 Q