3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = array( array( 'name' => 'Strawberry', 'id' => '10' ), array( 'id' => '13' ), array( 'name' => 'Apple', 'id' => '20' ), array( 'id' => '28' ), array( 'name' => 'Mandarin', 'id' => '30' ), array( 'name' => 'Apple', 'id' => '50' ), array( 'name' => 'Strawberry', 'id' => '60' ) ); $arraylength = count($array); $result = array();//仮の結果。結果として三次元配列になる。 $bararray = array();// idだけの配列を抜き出す。 for ($j = 0; $j < $arraylength; $j++) { if (array_key_exists('id', $array[$j]) == true && array_key_exists('name', $array[$j]) != true) { $newarr = array(); $newarr['id'] = $array[$j]['id']; array_push($bararray, $newarr); } }// idのみの配列を抜き出した。 $j = 0; for ($i = 0; $i < $arraylength; $i++) { $newarr = array(); for ($k = $i; $k < $arraylength; $k++) { if (array_key_exists('id', $array[$i]) == true && array_key_exists('name', $array[$i]) == true && array_key_exists('name', $array[$k]) == true && $array[$k]['name'] == $array[$i]['name']) { array_push($newarr, $array[$k]); if ($i != $k) { unset($array[$k]); $arraylength--; } ; } } if (!$newarr) { array_push($result, $bararray[$j]); $j++; } else { array_push($result, $newarr); } }//仮の結果、三次元配列はここまで print_r($result); $realresult = array();//求めたい結果 $resultlength = count($result); for ($k = 0; $k < $resultlength; $k++) { $resultArraylength = count($result[$k]); if ($resultArraylength == 1) { if (is_array($result[$k]) == true && array_key_exists(0, $result[$k]) == false) { array_push($realresult, $result[$k]); } else { array_push($realresult, $result[$k]['0']); } } else { for ($m = 0; $m < $resultArraylength; $m++) { array_push($realresult, $result[$k][$m]); } } } var_dump($realresult);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 7
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 81
Branch analysis from position: 81
2 jumps found. (Code = 44) Position 1 = 83, Position 2 = 31
Branch analysis from position: 83
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
2 jumps found. (Code = 44) Position 1 = 132, Position 2 = 91
Branch analysis from position: 132
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 91
2 jumps found. (Code = 43) Position 1 = 96, Position 2 = 118
Branch analysis from position: 96
2 jumps found. (Code = 46) Position 1 = 100, Position 2 = 104
Branch analysis from position: 100
2 jumps found. (Code = 43) Position 1 = 105, Position 2 = 111
Branch analysis from position: 105
1 jumps found. (Code = 42) Position 1 = 117
Branch analysis from position: 117
1 jumps found. (Code = 42) Position 1 = 129
Branch analysis from position: 129
2 jumps found. (Code = 44) Position 1 = 132, Position 2 = 91
Branch analysis from position: 132
Branch analysis from position: 91
Branch analysis from position: 111
1 jumps found. (Code = 42) Position 1 = 129
Branch analysis from position: 129
Branch analysis from position: 104
Branch analysis from position: 118
1 jumps found. (Code = 42) Position 1 = 127
Branch analysis from position: 127
2 jumps found. (Code = 44) Position 1 = 129, Position 2 = 120
Branch analysis from position: 129
Branch analysis from position: 120
2 jumps found. (Code = 44) Position 1 = 129, Position 2 = 120
Branch analysis from position: 129
Branch analysis from position: 120
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
2 jumps found. (Code = 44) Position 1 = 67, Position 2 = 34
Branch analysis from position: 67
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 76
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 80
Branch analysis from position: 80
2 jumps found. (Code = 44) Position 1 = 83, Position 2 = 31
Branch analysis from position: 83
Branch analysis from position: 31
Branch analysis from position: 76
2 jumps found. (Code = 44) Position 1 = 83, Position 2 = 31
Branch analysis from position: 83
Branch analysis from position: 31
Branch analysis from position: 34
2 jumps found. (Code = 46) Position 1 = 38, Position 2 = 42
Branch analysis from position: 38
2 jumps found. (Code = 46) Position 1 = 43, Position 2 = 47
Branch analysis from position: 43
2 jumps found. (Code = 46) Position 1 = 48, Position 2 = 54
Branch analysis from position: 48
2 jumps found. (Code = 43) Position 1 = 55, Position 2 = 64
Branch analysis from position: 55
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 64
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 67, Position 2 = 34
Branch analysis from position: 67
Branch analysis from position: 34
Branch analysis from position: 64
Branch analysis from position: 64
Branch analysis from position: 54
Branch analysis from position: 47
Branch analysis from position: 42
Branch analysis from position: 7
2 jumps found. (Code = 46) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 25
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 7
Branch analysis from position: 28
Branch analysis from position: 7
Branch analysis from position: 25
Branch analysis from position: 15
filename:       /in/a8JfA
function name:  (null)
number of ops:  136
compiled vars:  !0 = $array, !1 = $arraylength, !2 = $result, !3 = $bararray, !4 = $j, !5 = $newarr, !6 = $i, !7 = $k, !8 = $realresult, !9 = $resultlength, !10 = $resultArraylength, !11 = $m
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   31     1        COUNT                                            ~13     !0
          2        ASSIGN                                                   !1, ~13
   32     3        ASSIGN                                                   !2, <array>
   33     4        ASSIGN                                                   !3, <array>
   36     5        ASSIGN                                                   !4, 0
          6      > JMP                                                      ->26
   37     7    >   FETCH_DIM_R                                      ~18     !0, !4
          8        ARRAY_KEY_EXISTS                                 ~19     'id', ~18
          9        BOOL                                             ~20     ~19
         10      > JMPZ_EX                                          ~20     ~20, ->15
         11    >   FETCH_DIM_R                                      ~21     !0, !4
         12        ARRAY_KEY_EXISTS                                 ~22     'name', ~21
         13        BOOL_NOT                                         ~23     ~22
         14        BOOL                                             ~20     ~23
         15    > > JMPZ                                                     ~20, ->25
   38    16    >   ASSIGN                                                   !5, <array>
   39    17        FETCH_DIM_R                                      ~26     !0, !4
         18        FETCH_DIM_R                                      ~27     ~26, 'id'
         19        ASSIGN_DIM                                               !5, 'id'
         20        OP_DATA                                                  ~27
   40    21        INIT_FCALL                                               'array_push'
         22        SEND_REF                                                 !3
         23        SEND_VAR                                                 !5
         24        DO_ICALL                                                 
   36    25    >   PRE_INC                                                  !4
         26    >   IS_SMALLER                                               !4, !1
         27      > JMPNZ                                                    ~30, ->7
   43    28    >   ASSIGN                                                   !4, 0
   44    29        ASSIGN                                                   !6, 0
         30      > JMP                                                      ->81
   46    31    >   ASSIGN                                                   !5, <array>
   47    32        ASSIGN                                                   !7, !6
         33      > JMP                                                      ->65
   48    34    >   FETCH_DIM_R                                      ~35     !0, !6
         35        ARRAY_KEY_EXISTS                                 ~36     'id', ~35
         36        BOOL                                             ~37     ~36
         37      > JMPZ_EX                                          ~37     ~37, ->42
         38    >   FETCH_DIM_R                                      ~38     !0, !6
         39        ARRAY_KEY_EXISTS                                 ~39     'name', ~38
         40        BOOL                                             ~40     ~39
         41        BOOL                                             ~37     ~40
         42    > > JMPZ_EX                                          ~37     ~37, ->47
         43    >   FETCH_DIM_R                                      ~41     !0, !7
         44        ARRAY_KEY_EXISTS                                 ~42     'name', ~41
         45        BOOL                                             ~43     ~42
         46        BOOL                                             ~37     ~43
         47    > > JMPZ_EX                                          ~37     ~37, ->54
         48    >   FETCH_DIM_R                                      ~44     !0, !7
         49        FETCH_DIM_R                                      ~45     ~44, 'name'
         50        FETCH_DIM_R                                      ~46     !0, !6
         51        FETCH_DIM_R                                      ~47     ~46, 'name'
         52        IS_EQUAL                                         ~48     ~45, ~47
         53        BOOL                                             ~37     ~48
         54    > > JMPZ                                                     ~37, ->64
   49    55    >   INIT_FCALL                                               'array_push'
         56        SEND_REF                                                 !5
         57        FETCH_DIM_R                                      ~49     !0, !7
         58        SEND_VAL                                                 ~49
         59        DO_ICALL                                                 
   50    60        IS_NOT_EQUAL                                             !6, !7
         61      > JMPZ                                                     ~51, ->64
   51    62    >   UNSET_DIM                                                !0, !7
   52    63        PRE_DEC                                                  !1
   47    64    >   PRE_INC                                                  !7
         65    >   IS_SMALLER                                               !7, !1
         66      > JMPNZ                                                    ~54, ->34
   58    67    >   BOOL_NOT                                         ~55     !5
         68      > JMPZ                                                     ~55, ->76
   59    69    >   INIT_FCALL                                               'array_push'
         70        SEND_REF                                                 !2
         71        FETCH_DIM_R                                      ~56     !3, !4
         72        SEND_VAL                                                 ~56
         73        DO_ICALL                                                 
   60    74        PRE_INC                                                  !4
         75      > JMP                                                      ->80
   62    76    >   INIT_FCALL                                               'array_push'
         77        SEND_REF                                                 !2
         78        SEND_VAR                                                 !5
         79        DO_ICALL                                                 
   44    80    >   PRE_INC                                                  !6
         81    >   IS_SMALLER                                               !6, !1
         82      > JMPNZ                                                    ~61, ->31
   65    83    >   INIT_FCALL                                               'print_r'
         84        SEND_VAR                                                 !2
         85        DO_ICALL                                                 
   66    86        ASSIGN                                                   !8, <array>
   67    87        COUNT                                            ~64     !2
         88        ASSIGN                                                   !9, ~64
   68    89        ASSIGN                                                   !7, 0
         90      > JMP                                                      ->130
   69    91    >   FETCH_DIM_R                                      ~67     !2, !7
         92        COUNT                                            ~68     ~67
         93        ASSIGN                                                   !10, ~68
   70    94        IS_EQUAL                                                 !10, 1
         95      > JMPZ                                                     ~70, ->118
   71    96    >   FETCH_DIM_R                                      ~71     !2, !7
         97        TYPE_CHECK                                  128  ~72     ~71
         98        BOOL                                             ~73     ~72
         99      > JMPZ_EX                                          ~73     ~73, ->104
        100    >   FETCH_DIM_R                                      ~74     !2, !7
        101        ARRAY_KEY_EXISTS                                 ~75     0, ~74
        102        BOOL_NOT                                         ~76     ~75
        103        BOOL                                             ~73     ~76
        104    > > JMPZ                                                     ~73, ->111
   72   105    >   INIT_FCALL                                               'array_push'
        106        SEND_REF                                                 !8
        107        FETCH_DIM_R                                      ~77     !2, !7
        108        SEND_VAL                                                 ~77
        109        DO_ICALL                                                 
        110      > JMP                                                      ->117
   74   111    >   INIT_FCALL                                               'array_push'
        112        SEND_REF                                                 !8
        113        FETCH_DIM_R                                      ~79     !2, !7
        114        FETCH_DIM_R                                      ~80     ~79, 0
        115        SEND_VAL                                                 ~80
        116        DO_ICALL                                                 
        117    > > JMP                                                      ->129
   78   118    >   ASSIGN                                                   !11, 0
        119      > JMP                                                      ->127
   79   120    >   INIT_FCALL                                               'array_push'
        121        SEND_REF                                                 !8
        122        FETCH_DIM_R                                      ~83     !2, !7
        123        FETCH_DIM_R                                      ~84     ~83, !11
        124        SEND_VAL                                                 ~84
        125        DO_ICALL                                                 
   78   126        PRE_INC                                                  !11
        127    >   IS_SMALLER                                               !11, !10
        128      > JMPNZ                                                    ~87, ->120
   68   129    >   PRE_INC                                                  !7
        130    >   IS_SMALLER                                               !7, !9
        131      > JMPNZ                                                    ~89, ->91
   85   132    >   INIT_FCALL                                               'var_dump'
        133        SEND_VAR                                                 !8
        134        DO_ICALL                                                 
        135      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.01 ms | 1408 KiB | 19 Q