If I have an array like this:
$data[0] = ['a' => 'b'];
$data[1] = ['c' => 'd'];
$data[2] = ['e' => 'f'];
How do I add further data inside a specific key of the array whilst keeping the existing data, e.g.
$data[0] = ['a' => 'b'];
$data[1] = ['c' => 'd', 'xx' => 'zz']; // New data has been added here.
$data[2] = ['e' => 'f'];
How do I add things to $data[1]
for example?
I've read the following but these don't seem to be the answer:
- Push item to associative array in PHP
- How to push both value and key into array
- Insert new item in array on any position in PHP
I've looked at methods like array_combine()
, array_push()
and array_merge()
but cannot seem to do this. Apologies if this is an obvious question but I have tried to look up the things above and can't figure it out.