How to add elements to the beginning of an array in php
Apr 19, 2023 am 11:37 AMIn PHP, if we want to add an element at the beginning of an array, we can use the following methods.
- 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, ...)
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);
The output is:
Array ( [0] => pear [1] => grape [2] => banana [3] => apple [4] => orange )
- 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);
The output result is:
Array ( [0] => pear [1] => banana [2] => apple [3] => orange )
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.
- 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);
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the best practices for deduplication of PHP arrays

What Are the Latest PHP Coding Standards and Best Practices?

Can PHP array deduplication take advantage of key name uniqueness?

How Do I Work with PHP Extensions and PECL?

How to Implement message queues (RabbitMQ, Redis) in PHP?

Does PHP array deduplication need to be considered for performance losses?

What are the optimization techniques for deduplication of PHP arrays

How to Use Reflection to Analyze and Manipulate PHP Code?
