I've searched how to push both key and value and I found this : How to push both value and key into array
But my question is how to add more than one key and value into an array?
$somearray
:
Array (
[id] => 1645819602
[name] => Michael George)
I want to add this into $somearray
:
[first_name] => Michael
[last_name] => George
[work] => Google
So the output will be
Array (
[id] => 1645819602
[name] => Michael George
[first_name] => Michael
[last_name] => George
[work] => Google)
I know this code will not work
$arrayname[first_name] = Michael;
$arrayname[last_name] = George;
$arrayname[work] = Google;
Any help would be greatly appreciated. Thank you
Just assign $array[$key] = $value; It is automatically a push and a declaration at the same time.
To directly answer your question, no. PHP arrays can only contain one set of data for the key.
Definition and Usage. The array_push() function inserts one or more elements to the end of an array. Tip: You can add one value, or as many as you like. Note: Even if your array has string keys, your added elements will always have numeric keys (See example below).
The array_keys() function returns an array containing the keys.
You have to enclose the array key in quotes and also the value if its a string.If the value is an integer then there is no need of enclosing the value in quotes.But you must enclose the value in quotes if its a string.So you need to change he code like this
$arrayname['first_name'] = 'Michael';
$arrayname['last_name'] = 'George';
$arrayname['work'] = 'Google';
This will give you the idea:
<?
$array = array(
[id] => 1);
$array["hello"] = "world";
print_r($array); //prints Array (
[id] => 1,
[hello] => "world")
?>
Syntax for adding value into array,
$ArrayName['IndexName'] = $elementValue;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With