array_push() with key value pair

php array add key value pair to existing array
array_push with key
array_push associative array php
php array push key value foreach
php push array into array
php associative array push
add array to array php
array push with key javascript

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?

So what about having:


array_push - Manual, If you're going to use array_push() to insert a "$key" => "$value" pair into an array​, it can be done using the following: $data[$key] = $value; It is not necessary to  In the below program the array_push() function is used to push new elements in an array with no keys. In the below program, we will understand how the array_push() function works with an array having a already defined key_value pair.

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

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

How to push values with keys in to an array? - PHP, The query will return an set of values like this, C_OVERVIEW_LABEL Warning: array_push() [function.array-push]: First argument should be an array. jquery push array with key, create array with key and value in jquery, javascript array push key value pair dynamically, array push with specific key javascript, javascript array push dynamic key value

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

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

array_push, If you're going to use array_push() to insert a "$key" => "$value" pair into an array​, it can be done using the following: $data[$key] = $value; It is not necessary to  if two arrays have both the same key, array_merge and array union (+=) behave in the opposite way i.e. array_merge will respect the value from the second array and array union will respect the value from the first array. – santiago arizti Aug 28 '19 at 15:49

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


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


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

PHP array_push() Function, The array_push() function inserts one or more elements to the end of an array. Note: Even if your array has string keys, your added elements will always have numeric keys Specifies the value to add (Required in PHP versions before 7.3). Define Employee Array. Define key value pair employee object and push it into employees array. In below example we have employee keys say id , name and age with their respective values. To verify it just iterate the employee array and retrieve the each employee value with the help of keys.

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");


     [color1] => red
     [color2] => blue
     [color3] => green

PHP, Note: If the array has a key, value pair, then the method will always add a the array_push() function is used to push new elements in an array with no keys. But when I use the same code, the browser shows {{}} instead of showing the updated value of items (I am using angularjs btw). I don't know why is this happening. – exAres Sep 23 '13 at 8:33

PHP Add to Array, Here we will learn how to PHP array push with key, PHP add to an array push associative array PHP, PHP array add key-value pair to an  KeyValuePair. The crow pushes against the latch. The gate unlocks. Inside, it finds seed and nourishment. The action (a push with its beak) has a result (a value).

PHP array_push() Append Elements to an Array -- EndMemo, PHP array_push(). array_push() appends one or more elements to an array. want to append elements to an associative array, just define the key and values. supported by most of browsers, and it checks if object key available or not you want to add, if available it overides existing key value and it not available it add key with value example 1 let my_object = {}; // now i want to add something in it = "this is red color"; // { red : "this is red color"}

PHP array_push Function, Two new arrays are initialized containing some key-value pairs. However, on adding the two new arrays in the original array, new numeric keys  Thanks Stormrider. that worked great. but i read from the site on the function reference guide that to push values with keys we can use the format which i used. That's why i posted this

  • 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.
  • 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. :)
  • 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)