3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(-1); $array = array(array('id' => 1, 'fid' => 2), array('id' => 2, 'fid' => 2)); class Map { public static function byProperty(array $array, $property) { $map = array(); foreach ($array as $record) { $propValue = self::_getPropertyValue($record, $property); $map[$propValue][] = $record; } return $map; } public static function byMultiProperty(array $array, array $properties) { $map = array(); $lastProperty = end($property); foreach($array as $record) { $pointer = $map; foreach($properties as $property) { $propValue = self::_getPropertyValue($record, $property); if(!isset($pointer[$propValue])) { $pointer[$propValue] = array(); } $pointer = &$pointer[$propValue]; if($property === $lastProperty) { $pointer[] = $record; } } } return $map; } /** * @param mixed $record * @param string $property * @return mixed */ private static function _getPropertyValue($record, $property) { if (is_array($record)) { return isset($record[$property]) ? $record[$property] : null; } if (is_object($record)) { return isset($record->$property) ? $record->$property : null; } throw new InvalidArgumentException(sprintf('Unsupport record type: %s', gettype($record))); } } var_dump(Map::byMultiProperty($array, array('fid', 'id')));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/hsA2o
function name:  (null)
number of ops:  12
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   INIT_FCALL                                               'error_reporting'
          1        SEND_VAL                                                 -1
          2        DO_ICALL                                                 
    4     3        ASSIGN                                                   !0, <array>
   59     4        INIT_FCALL                                               'var_dump'
          5        INIT_STATIC_METHOD_CALL                                  'Map', 'byMultiProperty'
          6        SEND_VAR                                                 !0
          7        SEND_VAL                                                 <array>
          8        DO_FCALL                                      0  $3      
          9        SEND_VAR                                                 $3
         10        DO_ICALL                                                 
         11      > RETURN                                                   1

Class Map:
Function byproperty:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 14
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 14
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/hsA2o
function name:  byProperty
number of ops:  17
compiled vars:  !0 = $array, !1 = $property, !2 = $map, !3 = $record, !4 = $propValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   10     2        ASSIGN                                                   !2, <array>
   11     3      > FE_RESET_R                                       $6      !0, ->14
          4    > > FE_FETCH_R                                               $6, !3, ->14
   12     5    >   INIT_STATIC_METHOD_CALL                                  '_getPropertyValue'
          6        SEND_VAR_EX                                              !3
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0  $7      
          9        ASSIGN                                                   !4, $7
   13    10        FETCH_DIM_W                                      $9      !2, !4
         11        ASSIGN_DIM                                               $9
         12        OP_DATA                                                  !3
   11    13      > JMP                                                      ->4
         14    >   FE_FREE                                                  $6
   15    15      > RETURN                                                   !2
   16    16*     > RETURN                                                   null

End of function byproperty

Function bymultiproperty:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 31
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 31
Branch analysis from position: 9
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 29
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 29
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 28
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 28
Branch analysis from position: 22
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 29
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
filename:       /in/hsA2o
function name:  byMultiProperty
number of ops:  34
compiled vars:  !0 = $array, !1 = $properties, !2 = $map, !3 = $lastProperty, !4 = $property, !5 = $record, !6 = $pointer, !7 = $propValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   20     2        ASSIGN                                                   !2, <array>
   21     3        INIT_FCALL                                               'end'
          4        SEND_REF                                                 !4
          5        DO_ICALL                                         $9      
          6        ASSIGN                                                   !3, $9
   22     7      > FE_RESET_R                                       $11     !0, ->31
          8    > > FE_FETCH_R                                               $11, !5, ->31
   23     9    >   ASSIGN                                                   !6, !2
   24    10      > FE_RESET_R                                       $13     !1, ->29
         11    > > FE_FETCH_R                                               $13, !4, ->29
   25    12    >   INIT_STATIC_METHOD_CALL                                  '_getPropertyValue'
         13        SEND_VAR_EX                                              !5
         14        SEND_VAR_EX                                              !4
         15        DO_FCALL                                      0  $14     
         16        ASSIGN                                                   !7, $14
   26    17        ISSET_ISEMPTY_DIM_OBJ                         0  ~16     !6, !7
         18        BOOL_NOT                                         ~17     ~16
         19      > JMPZ                                                     ~17, ->22
   27    20    >   ASSIGN_DIM                                               !6, !7
         21        OP_DATA                                                  <array>
   29    22    >   FETCH_DIM_W                                      $19     !6, !7
         23        ASSIGN_REF                                               !6, $19
   31    24        IS_IDENTICAL                                             !4, !3
         25      > JMPZ                                                     ~21, ->28
   32    26    >   ASSIGN_DIM                                               !6
         27        OP_DATA                                                  !5
   24    28    > > JMP                                                      ->11
         29    >   FE_FREE                                                  $13
   22    30      > JMP                                                      ->8
         31    >   FE_FREE                                                  $11
   37    32      > RETURN                                                   !2
   38    33*     > RETURN                                                   null

End of function bymultiproperty

Function _getpropertyvalue:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 9
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 20
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/hsA2o
function name:  _getPropertyValue
number of ops:  30
compiled vars:  !0 = $record, !1 = $property
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   47     2        TYPE_CHECK                                  128          !0
          3      > JMPZ                                                     ~2, ->11
   48     4    >   ISSET_ISEMPTY_DIM_OBJ                         0          !0, !1
          5      > JMPZ                                                     ~3, ->9
          6    >   FETCH_DIM_R                                      ~4      !0, !1
          7        QM_ASSIGN                                        ~5      ~4
          8      > JMP                                                      ->10
          9    >   QM_ASSIGN                                        ~5      null
         10    > > RETURN                                                   ~5
   50    11    >   TYPE_CHECK                                  256          !0
         12      > JMPZ                                                     ~6, ->20
   51    13    >   ISSET_ISEMPTY_PROP_OBJ                                   !0, !1
         14      > JMPZ                                                     ~7, ->18
         15    >   FETCH_OBJ_R                                      ~8      !0, !1
         16        QM_ASSIGN                                        ~9      ~8
         17      > JMP                                                      ->19
         18    >   QM_ASSIGN                                        ~9      null
         19    > > RETURN                                                   ~9
   53    20    >   NEW                                              $10     'InvalidArgumentException'
         21        INIT_FCALL                                               'sprintf'
         22        SEND_VAL                                                 'Unsupport+record+type%3A+%25s'
   54    23        GET_TYPE                                         ~11     !0
         24        SEND_VAL                                                 ~11
         25        DO_ICALL                                         $12     
         26        SEND_VAR_NO_REF_EX                                       $12
         27        DO_FCALL                                      0          
         28      > THROW                                         0          $10
   55    29*     > RETURN                                                   null

End of function _getpropertyvalue

End of class Map.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
165.67 ms | 1404 KiB | 21 Q