Home > Backend Development > PHP Problem > How to add data to a new array in php

How to add data to a new array in php

PHPz
Release: 2023-04-20 15:40:22
Original
845 people have browsed it

PHP is a widely used server-side scripting language used for web development and building dynamic websites. In PHP programming, we often need to add data to a new array to facilitate various operations. This article will introduce how to add data to a new array in PHP.

Method 1: Use the array_push() function

The array_push() function is used to add one or more elements to the end of the array. This function returns the new length of the updated array.

Syntax:

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

Among them, array represents the array to which elements need to be added; value1, value2, ... represents the elements that need to be added to the array.

Example:

<?php

$students = array(&#39;Tom&#39;, &#39;John&#39;, &#39;Peter&#39;);

array_push($students, &#39;Lucy&#39;, &#39;James&#39;);

print_r($students);

?>
Copy after login

Output result:

Array
(
    [0] => Tom
    [1] => John
    [2] => Peter
    [3] => Lucy
    [4] => James
)
Copy after login
Copy after login

Method 2: Use the " " operator

In PHP, you can use the " " operator to Merge two arrays into a new array. If the same key name exists in two arrays, the elements in the second array will overwrite the elements in the first array.

Syntax:

$array = $array1 + $array2;
Copy after login

Among them, $array1 and $array2 are the two arrays to be merged.

Example:

<?php

$students1 = array(&#39;Tom&#39;, &#39;John&#39;, &#39;Peter&#39;);
$students2 = array(&#39;Lucy&#39;, &#39;James&#39;);

$students = $students1 + $students2;

print_r($students);

?>
Copy after login

Output result:

Array
(
    [0] => Tom
    [1] => John
    [2] => Peter
    [3] => Lucy
    [4] => James
)
Copy after login
Copy after login

Method 3: Direct assignment

In PHP, you can directly assign an element to a new array A specified key name. If the key name already exists, the element corresponding to the key name is overwritten.

Example:

<?php

$students = array();

$students[&#39;English&#39;] = &#39;Tom&#39;;
$students[&#39;Math&#39;] = &#39;John&#39;;
$students[&#39;Science&#39;] = &#39;Peter&#39;;

print_r($students);

?>
Copy after login

Output result:

Array
(
    [English] => Tom
    [Math] => John
    [Science] => Peter
)
Copy after login

The above are the three methods of adding data to a new array in PHP. Choosing the appropriate method according to actual needs can operate the array more efficiently.

The above is the detailed content of How to add data to a new 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