How to add elements to array in PHP

王林
Release: 2023-07-07 22:48:01
Original
1662 people have browsed it

How to add elements to an array in PHP

Array is a very commonly used data structure. In PHP, you can use the function array() or directly use square brackets [] to create an array. Arrays can be created with initial elements specified, or elements can be added later. This article will focus on how to add elements to an existing array.

In PHP, you can use the following methods to add elements to an array:

  1. Use the array_push() function

array_push() function Adds one or more elements to the end of an array. Its syntax is as follows:

array_push(array, value1, value2, ...)
Copy after login

Among them, array is the array of elements to be added, value1, value2, ... are the values ​​that need to be added. The following is an example:

$fruits = array("apple", "banana");
array_push($fruits, "orange", "grape");

print_r($fruits);
Copy after login

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => grape
)
Copy after login
Copy after login
Copy after login
Copy after login
  1. Use square brackets [] directly to add elements

In PHP, you can use it directly Square brackets [] are used to add elements to the end of the array. The following is a sample code:

$fruits = array("apple", "banana");
$fruits[] = "orange";
$fruits[] = "grape";

print_r($fruits);
Copy after login

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => grape
)
Copy after login
Copy after login
Copy after login
Copy after login
  1. Use the key name to specify the position to add elements

In addition to adding to the end, you can also add it by Adds an element to the array at the specified position by specifying the key name. The following is a sample code:

$fruits = array("apple", "banana");
$fruits[2] = "orange";
$fruits[3] = "grape";

print_r($fruits);
Copy after login

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => grape
)
Copy after login
Copy after login
Copy after login
Copy after login
  1. Use the array_merge() function to merge arrays

The array_merge() function can merge two Or combine multiple arrays into one array. You can add an element to an existing array by merging it with an array containing the element you want to add. The following is the sample code:

$fruits = array("apple", "banana");
$new_fruits = array("orange", "grape");

$fruits = array_merge($fruits, $new_fruits);

print_r($fruits);
Copy after login

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => grape
)
Copy after login
Copy after login
Copy after login
Copy after login

Summary:

This article introduces several common methods of adding elements to arrays in PHP. Use the array_push() function to add elements to the end of the array. Use square brackets [] to add elements directly to the end of the array. You can also add elements to specified positions by specifying key names. Finally, use the array_merge() function to merge arrays to achieve addition. Elemental effects. Select the appropriate method according to actual needs to conveniently handle the addition operation of the array.

I hope this article will help you understand how to add elements to an array in PHP!

The above is the detailed content of How to add elements to array in PHP. For more information, please follow other related articles on the PHP Chinese website!

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