3v4l.org

run code in 500+ PHP versions simultaneously
<?php $products = [ [ 'item_number' => 15678, 'description' => 'Tophat', 'size' => 'small', 'shelf' => 5, 'aisle' => 12, 'quantity' => 10, 'price' => 5.99 ], [ 'item_number' => 15644, 'description' => 'Something Else', 'size' => 'Large', 'shelf' => 6, 'aisle' => 10, 'quantity' => 99, 'price' => 8.99 ], [ 'item_number' => 15662, 'description' => 'T-shirt', 'size' => 'large', 'shelf' => 9, 'aisle' => 13, 'quantity' => 15, 'price' => null ] ]; function is_valid($product): bool { foreach ($product as $value) { if (!is_int($value) && !is_string($value) && !is_float($value)) { return false; } } return true; } function create_inventory(array $products): array { $inventory = []; // loop over your product array using the index to create an array if the product is valid. foreach ($products as $index => $product) { if (is_valid($product)) { // Create an empty array when the product is valid. $inventory[$index] = []; // Loop over the product values and add them to the newly created array. foreach ($product as $key => $value) { $inventory[$index][$key] = $value; } } } return $inventory; } var_dump(create_inventory($products));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LXtKG
function name:  (null)
number of ops:  8
compiled vars:  !0 = $products
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                       !0, <array>
   65     1        INIT_FCALL                                                   'var_dump'
          2        INIT_FCALL                                                   'create_inventory'
          3        SEND_VAR                                                     !0
          4        DO_FCALL                                          0  $2      
          5        SEND_VAR                                                     $2
          6        DO_ICALL                                                     
          7      > RETURN                                                       1

Function is_valid:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 17
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 17
Branch analysis from position: 3
2 jumps found. (Code = 46) Position 1 = 6, Position 2 = 9
Branch analysis from position: 6
2 jumps found. (Code = 46) Position 1 = 10, Position 2 = 13
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 16
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 13
Branch analysis from position: 9
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/LXtKG
function name:  is_valid
number of ops:  21
compiled vars:  !0 = $product, !1 = $value
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   38     0  E >   RECV                                                 !0      
   40     1      > FE_RESET_R                                           $2      !0, ->17
          2    > > FE_FETCH_R                                                   $2, !1, ->17
   41     3    >   TYPE_CHECK                                       16  ~3      !1
          4        BOOL_NOT                                             ~4      ~3
          5      > JMPZ_EX                                              ~4      ~4, ->9
          6    >   TYPE_CHECK                                       64  ~5      !1
          7        BOOL_NOT                                             ~6      ~5
          8        BOOL                                                 ~4      ~6
          9    > > JMPZ_EX                                              ~4      ~4, ->13
         10    >   TYPE_CHECK                                       32  ~7      !1
         11        BOOL_NOT                                             ~8      ~7
         12        BOOL                                                 ~4      ~8
         13    > > JMPZ                                                         ~4, ->16
   42    14    >   FE_FREE                                                      $2
         15      > RETURN                                                       <false>
   40    16    > > JMP                                                          ->2
         17    >   FE_FREE                                                      $2
   45    18      > RETURN                                                       <true>
   46    19*       VERIFY_RETURN_TYPE                                           
         20*     > RETURN                                                       null

End of function is_valid

Function create_inventory:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 20
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 20
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 19
Branch analysis from position: 9
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 18
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 18
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 18
Branch analysis from position: 19
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/LXtKG
function name:  create_inventory
number of ops:  25
compiled vars:  !0 = $products, !1 = $inventory, !2 = $product, !3 = $index, !4 = $value, !5 = $key
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   48     0  E >   RECV                                                 !0      
   50     1        ASSIGN                                                       !1, <array>
   52     2      > FE_RESET_R                                           $7      !0, ->20
          3    > > FE_FETCH_R                                           ~8      $7, !2, ->20
          4    >   ASSIGN                                                       !3, ~8
   53     5        INIT_FCALL                                                   'is_valid'
          6        SEND_VAR                                                     !2
          7        DO_FCALL                                          0  $10     
          8      > JMPZ                                                         $10, ->19
   55     9    >   ASSIGN_DIM                                                   !1, !3
         10        OP_DATA                                                      <array>
   57    11      > FE_RESET_R                                           $12     !2, ->18
         12    > > FE_FETCH_R                                           ~13     $12, !4, ->18
         13    >   ASSIGN                                                       !5, ~13
   58    14        FETCH_DIM_W                                          $15     !1, !3
         15        ASSIGN_DIM                                                   $15, !5
         16        OP_DATA                                                      !4
   57    17      > JMP                                                          ->12
         18    >   FE_FREE                                                      $12
   52    19    > > JMP                                                          ->3
         20    >   FE_FREE                                                      $7
   62    21        VERIFY_RETURN_TYPE                                           !1
         22      > RETURN                                                       !1
   63    23*       VERIFY_RETURN_TYPE                                           
         24*     > RETURN                                                       null

End of function create_inventory

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
170.02 ms | 1996 KiB | 16 Q