Add array to original array in php

王林
Release: 2023-05-06 09:35:07
Original
719 people have browsed it

In PHP, we usually use arrays to store and operate a set of data, and sometimes we need to combine multiple arrays into one array. This article will explain how to merge one array with another array and how to add elements to the original array.

1. Merge arrays

In PHP, you can use the array_merge() function to merge two or more arrays into one array. The syntax of the function is as follows:

array array_merge ( array $array1 [, array $... ] )
Copy after login

Among them, $array1 represents the first array to be merged, and the following $... represents more arrays to be merged.

The following is an example of merging two arrays $array1 and $array2 into one array:

$array1 = array('a', 'b', 'c');
$array2 = array('d', 'e', 'f');
$result = array_merge($array1, $array2);
print_r($result);
Copy after login

Run the above code, the output is as follows:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)
Copy after login

We can see After merging $array1 and $array2, we get a new array containing all elements.

It should be noted that when using the array_merge() function to merge arrays, if there are the same key names in the arrays, the later arrays will overwrite the previous arrays.

2. Adding elements to the original array

In PHP, you can use the array_push() function to add one or more elements to the end of the array. The syntax of the function is as follows:

int array_push ( array &$array , mixed $value1 [, mixed $... ]] )
Copy after login

Among them, $array represents the array to which elements are to be added, $value1 to $... represents the elements to be added, which can be one or more.

The following is an example, adding element 'd' to the array $array1:

$array1 = array('a', 'b', 'c');
array_push($array1, 'd');
print_r($array1);
Copy after login

Run the above code, the output is as follows:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)
Copy after login

We can see that A new element 'd' has been added to $array1.

It should be noted that the array_push() function can add multiple elements to the array at one time, and multiple elements can be placed in an array and passed to the function.

In addition to using the array_push() function, you can also add elements to the array in the following ways:

  1. Add elements directly using the array subscript, for example:
$array1 = array('a', 'b', 'c');
$array1[3] = 'd';
print_r($array1);
Copy after login

Run the above code, the output result is the same as in the previous example.

  1. Use the array_unshift() function to add one or more elements to the beginning of the array, for example:
$array1 = array('a', 'b', 'c');
array_unshift($array1, 'd');
print_r($array1);
Copy after login

Run the above code, the output is as follows:

Array
(
    [0] => d
    [1] => a
    [2] => b
    [3] => c
)
Copy after login

We can see that a new element 'd' is added to $array1 and it becomes the first element of the array.

Summary

This article introduces how to merge multiple arrays into one array and several ways to add elements to the original array. In actual development, these operations are very common and relatively simple and easy to understand. Hope this helps PHP beginners.

The above is the detailed content of Add array to original 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