3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array1 = ['user_firstname' => 'Jack', 'user_lastname' => 'Smith', 'user_address_1' => '123']; $data = []; foreach($array1 as $key=>$value){ $tokens = explode('_', $key, 2); // split the string in an array of strings, using _ as a delimitor. // the 2 is the maximun number of tokens to split the string into, look at explode() docs $keyName = end($keyName); // use last element of the array $data[$keyName] = $value; // assign value to key } print_r($data);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 18
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 18
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/9N3X8
function name:  (null)
number of ops:  23
compiled vars:  !0 = $array1, !1 = $data, !2 = $value, !3 = $key, !4 = $tokens, !5 = $keyName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                   !0, <array>
    5     1        ASSIGN                                                   !1, <array>
    7     2      > FE_RESET_R                                       $8      !0, ->18
          3    > > FE_FETCH_R                                       ~9      $8, !2, ->18
          4    >   ASSIGN                                                   !3, ~9
    9     5        INIT_FCALL                                               'explode'
          6        SEND_VAL                                                 '_'
          7        SEND_VAR                                                 !3
          8        SEND_VAL                                                 2
          9        DO_ICALL                                         $11     
         10        ASSIGN                                                   !4, $11
   11    11        INIT_FCALL                                               'end'
         12        SEND_REF                                                 !5
         13        DO_ICALL                                         $13     
         14        ASSIGN                                                   !5, $13
   12    15        ASSIGN_DIM                                               !1, !5
         16        OP_DATA                                                  !2
    7    17      > JMP                                                      ->3
         18    >   FE_FREE                                                  $8
   15    19        INIT_FCALL                                               'print_r'
         20        SEND_VAR                                                 !1
         21        DO_ICALL                                                 
         22      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.52 ms | 1395 KiB | 19 Q