Home > Backend Development > PHP Problem > How to add elements to php associative array

How to add elements to php associative array

青灯夜游
Release: 2023-03-09 20:04:02
Original
2625 people have browsed it

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);
?>
Copy after login

Output:

Array ( [front] => hello [0] => a [1] => B )
Copy after login

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);
?>
Copy after login

Output:

Array ( [front] => Hello [0] => a [1] => B )
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template