0

I've done some searches but can't seem to find someone with the same problem as I. I can't figure out how to change the key of an array that's beeing pushed into another array.

Example.


    $array1
    $array2

    array_push($array1, $array2);

    $array1 [
       "0" [
        //the data in array2
       ]
    ]

I want to change the key value where it says "0". Anyone know how I can do that?

Ramo Mislimi
  • 184
  • 1
  • 1
  • 11

2 Answers2

3

You can not change a key directly. Instead you would insert the same data under the new key and remove the old key.

For example:

$array['new_key'] = $array['old_key'];
unset($array['old_key']);

Alternatively, instead of using array_push(), you can set the array key directly:

$array1['new_key'] = $array2;

I encourage you to read the PHP Arrays docs as arrays are a foundational element of PHP.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
2

Maybe you can try this?

$array1['your-key'] = $array2;