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);
Output for git.master, git.master_jit, rfc.property-hooks
array(4) { [0]=> int(14498) [1]=> int(13398) [2]=> int(13298) [3]=> int(15398) } array(4) { [3]=> array(3) { [0]=> array(2) { ["id"]=> int(1) ["price"]=> int(11900) } [1]=> array(2) { ["id"]=> int(2) ["price"]=> int(499) } [2]=> array(2) { ["id"]=> int(6) ["price"]=> int(2999) } } [0]=> array(3) { [0]=> array(2) { ["id"]=> int(1) ["price"]=> int(11900) } [1]=> array(2) { ["id"]=> int(2) ["price"]=> int(499) } [2]=> array(2) { ["id"]=> int(3) ["price"]=> int(2099) } } [1]=> array(3) { [0]=> array(2) { ["id"]=> int(1) ["price"]=> int(11900) } [1]=> array(2) { ["id"]=> int(2) ["price"]=> int(499) } [2]=> array(2) { ["id"]=> int(4) ["price"]=> int(999) } } [2]=> array(3) { [0]=> array(2) { ["id"]=> int(1) ["price"]=> int(11900) } [1]=> array(2) { ["id"]=> int(2) ["price"]=> int(499) } [2]=> array(2) { ["id"]=> int(5) ["price"]=> int(899) } } }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
89.78 ms | 409 KiB | 5 Q