Home Backend Development PHP Problem How to add elements to the beginning of an array in php

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

Apr 19, 2023 am 11:37 AM

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the best practices for deduplication of PHP arrays What are the best practices for deduplication of PHP arrays Mar 03, 2025 pm 04:41 PM

What are the best practices for deduplication of PHP arrays

What Are the Latest PHP Coding Standards and Best Practices? What Are the Latest PHP Coding Standards and Best Practices? Mar 10, 2025 pm 06:16 PM

What Are the Latest PHP Coding Standards and Best Practices?

Can PHP array deduplication take advantage of key name uniqueness? Can PHP array deduplication take advantage of key name uniqueness? Mar 03, 2025 pm 04:51 PM

Can PHP array deduplication take advantage of key name uniqueness?

How Do I Work with PHP Extensions and PECL? How Do I Work with PHP Extensions and PECL? Mar 10, 2025 pm 06:12 PM

How Do I Work with PHP Extensions and PECL?

How to Implement message queues (RabbitMQ, Redis) in PHP? How to Implement message queues (RabbitMQ, Redis) in PHP? Mar 10, 2025 pm 06:15 PM

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

Does PHP array deduplication need to be considered for performance losses? Does PHP array deduplication need to be considered for performance losses? Mar 03, 2025 pm 04:47 PM

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

What are the optimization techniques for deduplication of PHP arrays What are the optimization techniques for deduplication of PHP arrays Mar 03, 2025 pm 04:50 PM

What are the optimization techniques for deduplication of PHP arrays

How to Use Reflection to Analyze and Manipulate PHP Code? How to Use Reflection to Analyze and Manipulate PHP Code? Mar 10, 2025 pm 06:12 PM

How to Use Reflection to Analyze and Manipulate PHP Code?

See all articles