0

I have an array:

$array = [
'foo' => 44,
'bar' => 77,
'moo' => 88,
];

Now I have two variables, $key and $value, and I want to replace the first index of the array. So remove foo and 44, and add $key and $value in their place.

So my array becomes:

$array = [
 $key => $foo,
 'bar' => 77,
 'moo' => 88,
];

How may I do this?

ditto
  • 5,917
  • 10
  • 51
  • 88

1 Answers1

1

Remove the first element of the array:

array_shift($array);

Add the new key/value pair to the start of the array:

$array = array($key => $value) + $array;
rjdown
  • 9,162
  • 3
  • 32
  • 45