site design / logo © 2020 Stack Exchange Inc; user contributions licensed under So it was a stupid question since assoc arrays already have a key!! The array_push() function inserts one or more elements to the end of an array. It has not numeric arrays like they defined in other languages. Description. Teams. Stack Overflow for Teams is a private, secure spot for you and Stack Overflow works best with JavaScript enabled I was just giving the idea. RegardsThis doesn't work if you don't want to keep everything associative, e.g. Example:Then you can push anything to your array, here I try with an associative arrayResult: Array ( [0] => Array ( [id] => 23 [category] => aaa ) )Thanks for contributing an answer to Stack Overflow! Featured on Meta

After all it's in my interests too! without pushing other numbered arrays in between. Will do next time. Thanks.that code looks really messy, would you tell us what you're trying to do?I had no luck with Ariz's method, I don't think it is correct. ... Further Modification on the array_push_associative function 1. removes seemingly useless array_unshift function that generates php warning 2. adds support for non-array arguments

If you don't want to overwrite anything that might already be at 'name', you can also do something like this:Curtis's answer was very close to what I needed, but I changed it up a little. An assoc array doesn't rely on its numbering system so just adding another pair is the best way!

Where developers & technologists share private knowledge with coworkersProgramming & related technical career opportunitiesIt is not possible push an array into another array.

How we built it: our new Articles feature for Stack Overflow Teams. PHP array_push() function is used to insert new items at the end of an array and get the updated number of array elements. Add or Insert elements / values to an array In PHP. Tip: You can add one value, or as many as you like.

For example, to store the marks of different subject of a student in an array, a numerically indexed array would not be the best choice. Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices.

PHP array push() function has been introduced in PHP 4. site design / logo © 2020 Stack Exchange Inc; user contributions licensed under

your coworkers to find and share information.

See the answers below for the proper syntax of i'm trying to use array push but for an assoc array not a numeric arrayphp has assoc arrays. Free 30 Day Trial Here we will take some examples, like add values in PHP arrays, PHP push arrays with keys, PHP add to an associative array, PHP add to the multidimensional arrays, PHP associative push arrays, PHP arrays add key-value pairs to an existing an array. Associative array − An array with strings as index.

The Overflow Blog Podcast 259: from web comics to React core with Rachel Nabors. It created the index but would not set the value.

You may add as many values as you need. Stack Overflow for Teams is a private, secure spot for you and Here you have learned how to add values in array PHP, PHP array push with key, PHP add to an associative array, PHP add to the multidimensional array, array push associative array PHP, PHP array adds key-value pair to an existing array with examples. In a similar way, the associative array can be sorted by key alphabetically both in ascending order and in descending order as shown in the below example. Featured on Meta Sorting of Associative Array by Key in PHP. I solved my problem with a Class.actually it should be $existing_array = array('a'=>'b', 'b'=>'c'); $new_array = array('d'=>'e', 'f'=>'g'); $result = array_merge($existing_array, $new_array);well it is a common sense to collect output. Thanks for your time anyway!Clever, Was looking for a way to push a mix of asssociative and incremental index into one single array and keep the order they've been 'pushed. Just change few snippet(use array_merge function):-If the array $arr_options contains the existing array.Warning: $arr_options must exist.

By using our site, you acknowledge that you have read and understand our