Home php How to add an array element with a key?

How to add an array element with a key?

Author

Date

Category

In order not to change all the array keys, such as, for example, using Array_unshift .


Answer 1, Authority 100%

Arrays:

$ arr = array ('a' = & gt; 'abc', 'b' = & gt; 'def' ); // Sainted
$ add = array ('c' = & gt; 'ghi'); // Added
$ arr = $ add + $ arr; // fold

Result:

array
(
  [c] = & gt; GHI.
  [a] = & gt; ABC
  [b] = & gt; DEF.
)

p.s. By the way, Array_Merge () is also quite normally working:

$ arr = array_merge ($ add, $ arr); // The result is the same

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions