Please note that this is not a duplicate - but an extension of these questions below)
PHP array_push one array into another
array_push into a multi-dimensional array
I am trying to array_push into a multidimensional array but want to keep the array key of the second array.
Example:
<?php
$samplearray = array(
array('name' => "Joe Bloggs", 'age' => "30", 'sex' => "Male", 'title' => "Mr" ),
array('name' => "Jane Bloggs", 'age' => "30", 'sex' => "Female", 'title' => "Mrs" ),
array('name' => "Little Bloggs", 'age' => "10", 'sex' => "Male", 'title' => "Master" ),
);
array_push ($samplearray[0],"Inserted Value");
print_r($samplearray);
?>
The output for this is:
array(3) {
[0]=>
array(5) {
["name"]=>
string(10) "Joe Bloggs"
["age"]=>
string(2) "30"
["sex"]=>
string(4) "Male"
["title"]=>
string(2) "Mr"
[0]=>
string(14) "Inserted Value" <-- INSERTED VALUE
}
[1]=>
array(4) {
["name"]=>
string(11) "Jane Bloggs"
["age"]=>
string(2) "30"
["sex"]=>
string(6) "Female"
["title"]=>
string(3) "Mrs"
}
[2]=>
array(4) {
["name"]=>
string(13) "Little Bloggs"
["age"]=>
string(2) "10"
["sex"]=>
string(4) "Male"
["title"]=>
string(6) "Master"
}
}
I want to insert a key along with the value but when I try that - it fails. Can you please advice
array_push ($samplearray[0]['insertedvalue'],"Inserted Value");
returns a NULL value for the inserted key on local server but fails on PHPfiddle.