Home > Article > Backend Development > How to add elements to php associative array
How to add elements to an associative array in php: 1. Use the array_merge() function to merge two associative arrays to add elements. The syntax is "array_merge(associative array 1, associative array 2)"; 2. Use " " Operator, combines two associative arrays to add elements, the syntax is "associative array 1 associative array 2".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
1. Use the array_merge() function
<?php $queue = array('a', 'B'); $queue = array_merge(array('front' => 'hello'), $queue); print_r($queue); ?>
Output:
Array ( [front] => hello [0] => a [1] => B )
Description:
array_merge() function can merge one or more arrays into one array.
2. Use the " " operator
<?php $queue = array('a', 'B'); $queue = array('front' => 'Hello') + $queue; print_r($queue); ?>
Output:
Array ( [front] => hello [0] => a [1] => B )
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of How to add elements to php associative array. For more information, please follow other related articles on the PHP Chinese website!