3v4l.org

run code in 300+ PHP versions simultaneously
<?php function __( $str ) { return $str; } function _doing_it_wrong( $func, $error_message, $version ) { trigger_error( $error_message, E_USER_NOTICE ); } class WP_List_Util { /** * The input array. * * @since 4.7.0 * @var array */ private $input = array(); /** * The output array. * * @since 4.7.0 * @var array */ private $output = array(); /** * Temporary arguments for sorting. * * @since 4.7.0 * @var string[] */ private $orderby = array(); /** * Constructor. * * Sets the input array. * * @since 4.7.0 * * @param array $input Array to perform operations on. */ public function __construct( $input ) { $this->output = $input; $this->input = $input; } public function pluck( $field, $index_key = null ) { $newlist = array(); if ( ! $index_key ) { /* * This is simple. Could at some point wrap array_column() * if we knew we had an array of arrays. */ foreach ( $this->output as $key => $value ) { if ( is_object( $value ) ) { $newlist[ $key ] = $value->$field; } elseif ( is_array( $value ) ) { $newlist[ $key ] = $value[ $field ]; } else { _doing_it_wrong( __METHOD__, __( 'Values for the input array must be either objects or arrays.' ), '6.2.0' ); } } $this->output = $newlist; return $this->output; } /* * When index_key is not set for a particular item, push the value * to the end of the stack. This is how array_column() behaves. */ foreach ( $this->output as $value ) { if ( is_object( $value ) ) { if ( isset( $value->$index_key ) ) { $newlist[ $value->$index_key ] = $value->$field; } else { $newlist[] = $value->$field; } } elseif ( is_array( $value ) ) { if ( isset( $value[ $index_key ] ) ) { $newlist[ $value[ $index_key ] ] = $value[ $field ]; } else { $newlist[] = $value[ $field ]; } } else { // _doing_it_wrong( // __METHOD__, // __( 'Values for the input array must be either objects or arrays.' ), // '6.2.0' // ); } } $this->output = $newlist; return $this->output; } } function wp_list_pluck( $input_list, $field, $index_key = null ) { if ( ! is_array( $input_list ) ) { return array(); } $util = new WP_List_Util( $input_list ); return $util->pluck( $field, $index_key ); } /* * Test with an array of arrays. */ $input_list = array( array( '123' => '456' ), array( 'foo' => 'bar' ), array( 'foo' => 'baz' ), ); var_dump( array_column( $input_list, 'foo', null ) ); var_dump( wp_list_pluck( $input_list, 'foo', null ) ); /* * Test with an array of objects. */ $input_list = array( (object) array( '123' => '456' ), (object) array( 'foo' => 'bar' ), (object) array( 'foo' => 'baz' ), ); var_dump( wp_list_pluck( $input_list, 'foo', null ) );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RtbZJ
function name:  (null)
number of ops:  33
compiled vars:  !0 = $input_list
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  122     0  E >   ASSIGN                                                   !0, <array>
  127     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'array_column'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 'foo'
          5        SEND_VAL                                                 null
          6        DO_ICALL                                         $2      
          7        SEND_VAR                                                 $2
          8        DO_ICALL                                                 
  128     9        INIT_FCALL                                               'var_dump'
         10        INIT_FCALL                                               'wp_list_pluck'
         11        SEND_VAR                                                 !0
         12        SEND_VAL                                                 'foo'
         13        SEND_VAL                                                 null
         14        DO_FCALL                                      0  $4      
         15        SEND_VAR                                                 $4
         16        DO_ICALL                                                 
  134    17        CAST                                          8  ~6      <array>
         18        INIT_ARRAY                                       ~7      ~6
  135    19        CAST                                          8  ~8      <array>
         20        ADD_ARRAY_ELEMENT                                ~7      ~8
  136    21        CAST                                          8  ~9      <array>
         22        ADD_ARRAY_ELEMENT                                ~7      ~9
  133    23        ASSIGN                                                   !0, ~7
  138    24        INIT_FCALL                                               'var_dump'
         25        INIT_FCALL                                               'wp_list_pluck'
         26        SEND_VAR                                                 !0
         27        SEND_VAL                                                 'foo'
         28        SEND_VAL                                                 null
         29        DO_FCALL                                      0  $11     
         30        SEND_VAR                                                 $11
         31        DO_ICALL                                                 
         32      > RETURN                                                   1

Function __:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RtbZJ
function name:  __
number of ops:  3
compiled vars:  !0 = $str
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1      > RETURN                                                   !0
    5     2*     > RETURN                                                   null

End of function __

Function _doing_it_wrong:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RtbZJ
function name:  _doing_it_wrong
number of ops:  8
compiled vars:  !0 = $func, !1 = $error_message, !2 = $version
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
    8     3        INIT_FCALL                                               'trigger_error'
          4        SEND_VAR                                                 !1
          5        SEND_VAL                                                 1024
          6        DO_ICALL                                                 
    9     7      > RETURN                                                   null

End of function _doing_it_wrong

Function wp_list_pluck:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RtbZJ
function name:  wp_list_pluck
number of ops:  17
compiled vars:  !0 = $input_list, !1 = $field, !2 = $index_key, !3 = $util
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  109     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
  110     3        TYPE_CHECK                                  128  ~4      !0
          4        BOOL_NOT                                         ~5      ~4
          5      > JMPZ                                                     ~5, ->7
  111     6    > > RETURN                                                   <array>
  114     7    >   NEW                                              $6      'WP_List_Util'
          8        SEND_VAR_EX                                              !0
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !3, $6
  116    11        INIT_METHOD_CALL                                         !3, 'pluck'
         12        SEND_VAR_EX                                              !1
         13        SEND_VAR_EX                                              !2
         14        DO_FCALL                                      0  $9      
         15      > RETURN                                                   $9
  117    16*     > RETURN                                                   null

End of function wp_list_pluck

Class WP_List_Util:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RtbZJ
function name:  __construct
number of ops:  6
compiled vars:  !0 = $input
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
   46     1        ASSIGN_OBJ                                               'output'
          2        OP_DATA                                                  !0
   47     3        ASSIGN_OBJ                                               'input'
          4        OP_DATA                                                  !0
   48     5      > RETURN                                                   null

End of function __construct

Function pluck:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 35
Branch analysis from position: 5
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 30
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 30
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 21
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
Branch analysis from position: 35
2 jumps found. (Code = 77) Position 1 = 37, Position 2 = 65
Branch analysis from position: 37
2 jumps found. (Code = 78) Position 1 = 38, Position 2 = 65
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 40, Position 2 = 51
Branch analysis from position: 40
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 47
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 64
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 64
Branch analysis from position: 64
Branch analysis from position: 51
2 jumps found. (Code = 43) Position 1 = 53, Position 2 = 64
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 55, Position 2 = 60
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
1 jumps found. (Code = 42) Position 1 = 64
Branch analysis from position: 64
Branch analysis from position: 60
1 jumps found. (Code = 42) Position 1 = 64
Branch analysis from position: 64
Branch analysis from position: 64
Branch analysis from position: 65
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 65
filename:       /in/RtbZJ
function name:  pluck
number of ops:  71
compiled vars:  !0 = $field, !1 = $index_key, !2 = $newlist, !3 = $value, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
   51     2        ASSIGN                                                   !2, <array>
   53     3        BOOL_NOT                                         ~6      !1
          4      > JMPZ                                                     ~6, ->35
   58     5    >   FETCH_OBJ_R                                      ~7      'output'
          6      > FE_RESET_R                                       $8      ~7, ->30
          7    > > FE_FETCH_R                                       ~9      $8, !3, ->30
          8    >   ASSIGN                                                   !4, ~9
   59     9        TYPE_CHECK                                  256          !3
         10      > JMPZ                                                     ~11, ->15
   60    11    >   FETCH_OBJ_R                                      ~13     !3, !0
         12        ASSIGN_DIM                                               !2, !4
         13        OP_DATA                                                  ~13
   59    14      > JMP                                                      ->29
   61    15    >   TYPE_CHECK                                  128          !3
         16      > JMPZ                                                     ~14, ->21
   62    17    >   FETCH_DIM_R                                      ~16     !3, !0
         18        ASSIGN_DIM                                               !2, !4
         19        OP_DATA                                                  ~16
   61    20      > JMP                                                      ->29
   64    21    >   INIT_FCALL                                               '_doing_it_wrong'
   65    22        SEND_VAL                                                 'WP_List_Util%3A%3Apluck'
   66    23        INIT_FCALL                                               '__'
         24        SEND_VAL                                                 'Values+for+the+input+array+must+be+either+objects+or+arrays.'
         25        DO_FCALL                                      0  $17     
         26        SEND_VAR                                                 $17
   67    27        SEND_VAL                                                 '6.2.0'
   64    28        DO_FCALL                                      0          
   58    29    > > JMP                                                      ->7
         30    >   FE_FREE                                                  $8
   72    31        ASSIGN_OBJ                                               'output'
         32        OP_DATA                                                  !2
   74    33        FETCH_OBJ_R                                      ~20     'output'
         34      > RETURN                                                   ~20
   81    35    >   FETCH_OBJ_R                                      ~21     'output'
         36      > FE_RESET_R                                       $22     ~21, ->65
         37    > > FE_FETCH_R                                               $22, !3, ->65
   82    38    >   TYPE_CHECK                                  256          !3
         39      > JMPZ                                                     ~23, ->51
   83    40    >   ISSET_ISEMPTY_PROP_OBJ                                   !3, !1
         41      > JMPZ                                                     ~24, ->47
   84    42    >   FETCH_OBJ_R                                      ~25     !3, !1
         43        FETCH_OBJ_R                                      ~27     !3, !0
         44        ASSIGN_DIM                                               !2, ~25
         45        OP_DATA                                                  ~27
   83    46      > JMP                                                      ->50
   86    47    >   FETCH_OBJ_R                                      ~29     !3, !0
         48        ASSIGN_DIM                                               !2
         49        OP_DATA                                                  ~29
   82    50    > > JMP                                                      ->64
   88    51    >   TYPE_CHECK                                  128          !3
         52      > JMPZ                                                     ~30, ->64
   89    53    >   ISSET_ISEMPTY_DIM_OBJ                         0          !3, !1
         54      > JMPZ                                                     ~31, ->60
   90    55    >   FETCH_DIM_R                                      ~32     !3, !1
         56        FETCH_DIM_R                                      ~34     !3, !0
         57        ASSIGN_DIM                                               !2, ~32
         58        OP_DATA                                                  ~34
   89    59      > JMP                                                      ->63
   92    60    >   FETCH_DIM_R                                      ~36     !3, !0
         61        ASSIGN_DIM                                               !2
         62        OP_DATA                                                  ~36
   88    63    > > JMP                                                      ->64
   81    64    > > JMP                                                      ->37
         65    >   FE_FREE                                                  $22
  103    66        ASSIGN_OBJ                                               'output'
         67        OP_DATA                                                  !2
  105    68        FETCH_OBJ_R                                      ~38     'output'
         69      > RETURN                                                   ~38
  106    70*     > RETURN                                                   null

End of function pluck

End of class WP_List_Util.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.41 ms | 1023 KiB | 20 Q