Home  >  Article  >  Backend Development  >  How to add elements to php associative array

How to add elements to php associative array

青灯夜游
青灯夜游Original
2021-05-20 17:55:302529browse

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".

How to add elements to php associative array

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

1. Use the array_merge() function

<?php
$queue = array(&#39;a&#39;, &#39;B&#39;);
$queue = array_merge(array(&#39;front&#39; => &#39;hello&#39;), $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(&#39;a&#39;, &#39;B&#39;);
$queue = array(&#39;front&#39; => &#39;Hello&#39;) + $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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn