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);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sNluJ
function name:  (null)
number of ops:  22
compiled vars:  !0 = $json, !1 = $sums
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   INIT_FCALL                                               'json_decode'
          1        SEND_VAL                                                 '%5B%0A++++%5B%7B%22id%22%3A1%2C%22price%22%3A11900%7D%2C%7B%22id%22%3A2%2C%22price%22%3A499%7D%2C%7B%22id%22%3A3%2C%22price%22%3A2099%7D%5D%2C%0A++++%5B%7B%22id%22%3A1%2C%22price%22%3A11900%7D%2C%7B%22id%22%3A2%2C%22price%22%3A499%7D%2C%7B%22id%22%3A4%2C%22price%22%3A999%7D%5D%2C%0A++++%5B%7B%22id%22%3A1%2C%22price%22%3A11900%7D%2C%7B%22id%22%3A2%2C%22price%22%3A499%7D%2C%7B%22id%22%3A5%2C%22price%22%3A899%7D%5D%2C%0A++++%5B%7B%22id%22%3A1%2C%22price%22%3A11900%7D%2C%7B%22id%22%3A2%2C%22price%22%3A499%7D%2C%7B%22id%22%3A6%2C%22price%22%3A2999%7D%5D%0A++++%5D'
   10     2        SEND_VAL                                                 <true>
    5     3        DO_ICALL                                         $2      
          4        ASSIGN                                                   !0, $2
   14     5        INIT_FCALL                                               'array_map'
          6        DECLARE_LAMBDA_FUNCTION                          ~4      [0]
          7        SEND_VAL                                                 ~4
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $5      
         10        ASSIGN                                                   !1, $5
   18    11        INIT_FCALL                                               'uksort'
         12        SEND_REF                                                 !0
         13        DECLARE_LAMBDA_FUNCTION                          ~7      [1]
         14        BIND_LEXICAL                                             ~7, !1
   20    15        SEND_VAL                                                 ~7
   18    16        DO_ICALL                                                 
   24    17        INIT_FCALL                                               'var_dump'
         18        SEND_VAR                                                 !1
         19        SEND_VAR                                                 !0
         20        DO_ICALL                                                 
         21      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sNluJ
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        INIT_FCALL                                               'array_sum'
          2        INIT_FCALL                                               'array_column'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 'price'
          5        DO_ICALL                                         $1      
          6        SEND_VAR                                                 $1
          7        DO_ICALL                                         $2      
          8      > RETURN                                                   $2
          9*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sNluJ
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $b, !1 = $a, !2 = $sums
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
   19     3        FETCH_DIM_R                                      ~3      !2, !1
          4        FETCH_DIM_R                                      ~4      !2, !0
          5        SPACESHIP                                        ~5      ~3, ~4
          6      > RETURN                                                   ~5
   20     7*     > RETURN                                                   null

End of Dynamic Function 1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.07 ms | 1021 KiB | 19 Q