How to add elements to the beginning of an array in php

PHPz
Release: 2023-04-19 13:57:37
Original
3812 people have browsed it

In PHP, if we want to add an element at the beginning of an array, we can use the following methods.

  1. Using the array_unshift() function

The array_unshift() function adds one or more elements to the beginning of an array. Its syntax is as follows:

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

Among them, array represents the array to which elements are to be added, value1, value2, etc. represent the elements to be added.

For example, the following code adds two elements to the beginning of an array:

$fruits = array("banana", "apple", "orange");
array_unshift($fruits, "pear", "grape");
print_r($fruits);
Copy after login

The output is:

Array
(
    [0] => pear
    [1] => grape
    [2] => banana
    [3] => apple
    [4] => orange
)
Copy after login
  1. Use the " " operator

In PHP, the " " operator can be used to merge two arrays. If you add an array to another array that contains only one element, that element will be added to the beginning of the original array.

For example, the following code uses the " " operator to add an element to the beginning of an array:

$fruits = array("banana", "apple", "orange");
$new_fruits = array("pear");
$fruits = $new_fruits + $fruits;
print_r($fruits);
Copy after login

The output result is:

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

It should be noted that if the two If the same key exists in two arrays, the " " operator will retain the elements in the left array and ignore the elements with the same key in the right array. Therefore, this approach may cause the keys of elements to be reordered.

  1. Using the array_merge() function

The array_merge() function can also be used to merge two arrays. Unlike the " " operator, it retains all elements from both arrays and combines them into a new array. If the same key exists in both arrays, the element with the same key in the array on the right overwrites the corresponding element in the array on the left.

For example, the following code uses the array_merge() function to add an element to the beginning of an array:

$fruits = array("banana", "apple", "orange");
$new_fruits = array("pear");
$fruits = array_merge($new_fruits, $fruits);
print_r($fruits);
Copy after login

The output is the same as using the " " operator.

It should be noted that when using the array_merge() function, if there are two or more elements with the same key, the last element in the array will overwrite the previous elements.

The above is the detailed content of How to add elements to the beginning of an 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!