203

I have an existing array to which I want to add a value.

I'm trying to achieve that using array_push() to no avail.

Below is my code:

$data = array(
    "dog" => "cat"
);

array_push($data['cat'], 'wagon');

What I want to achieve is to add cat as a key to the $data array with wagon as value so as to access it as in the snippet below:

echo $data['cat']; // the expected output is: wagon

How can I achieve that?

0

8 Answers 8

381

So what about having:

$data['cat']='wagon';
Sign up to request clarification or add additional context in comments.

3 Comments

Warning: $a['123'] = 456; - string '123' is converted to integer key 123.
For integer keys you don't want to wrap them into quotes / string.
Not necessarily. If some other keys are strings like '123a' it could be desired to preserve string keys for all items.
60

If you need to add multiple key=>value, then try this.

$data = array_merge($data, array("cat"=>"wagon","foo"=>"baar"));

3 Comments

Could also use a loop for that.
A loop does not help. If you want to push values with a duplicate key name, you must use array_merge.
When merging two associative arrays, array union syntax can be used +. More concisely, the array union assignment operator +=.
44
$data['cat'] = 'wagon';

That's all you need to add the key and value to the array.

Comments

8

You don't need to use array_push() function, you can assign new value with new key directly to the array like..

$array = array("color1"=>"red", "color2"=>"blue");
$array['color3']='green';
print_r($array);


Output:

   Array(
     [color1] => red
     [color2] => blue
     [color3] => green
   )

3 Comments

You should also highlight what's the exact difference, here you used = instead of , for OP
This code is wrong. array_push takes two parameters, and you will be getting a warning about the fact you're using it wrong with the result that the call to array_push does nothing. What the second line of your code is actually doing is simply $array['color3']='green'. That's exactly what @dusoft did above. Your code is just an obfuscation of that solution.
@RichardSmith thank you for showing my mistake, I have changed my answer. :)
7

For Example:

$data = array('firstKey' => 'firstValue', 'secondKey' => 'secondValue');

For changing key value:

$data['firstKey'] = 'changedValue'; 
//this will change value of firstKey because firstkey is available in array

output:

Array ( [firstKey] => changedValue [secondKey] => secondValue )

For adding new key value pair:

$data['newKey'] = 'newValue'; 
//this will add new key and value because newKey is not available in array

output:

Array ( [firstKey] => firstValue [secondKey] => secondValue [newKey] => newValue )

Comments

5

Array['key'] = value;

$data['cat'] = 'wagon';

This is what you need. No need to use array_push() function for this. Some time the problem is very simple and we think in complex way :) .

Comments

0
<?php
$data = ['name' => 'Bilal', 'education' => 'CS']; 
$data['business'] = 'IT';  //append new value with key in array
print_r($data);
?>

Result

Array
(
    [name] => Bilal
    [education] => CS
    [business] => IT
)

Comments

-4

Just do that:

$data = [
    "dog" => "cat"
];

array_push($data, ['cat' => 'wagon']);

*In php 7 and higher, array is creating using [], not ()

1 Comment

Two problems: array_push adds its 2nd+ parameters as new values (not key-value pairings as array_merge does), and PHP 7 happily accepts the array() array syntax (as well as the shorthand [] syntax)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.