Backend Development 4 min read

Common PHP Array Functions: array_push, array_pop, array_shift, array_unshift, and array_reverse

This article introduces several essential PHP array functions—including array_push, array_pop, array_shift, array_unshift, and array_reverse—explaining their usage, parameters, and effects with clear code examples to help developers manipulate arrays efficiently.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Common PHP Array Functions: array_push, array_pop, array_shift, array_unshift, and array_reverse

In PHP development, arrays are a fundamental data structure, and PHP offers a rich set of built-in functions to manipulate them. This article details several commonly used array functions, providing examples and explanations to help readers understand and apply them effectively.

array_push() function

The array_push() function adds one or more elements to the end of an array. It takes the target array as the first argument and the element(s) to add as subsequent arguments.

<code>$fruits = array("apple", "banana", "orange");
array_push($fruits, "watermelon");
</code>

After execution, $fruits becomes ["apple", "banana", "orange", "watermelon"] .

array_pop() function

The array_pop() function removes and returns the last element of an array. It accepts a single argument—the array to operate on.

<code>$fruits = array("apple", "banana", "orange");
$lastFruit = array_pop($fruits);
</code>

After execution, $lastFruit holds "orange", and $fruits becomes ["apple", "banana"] .

array_shift() function

The array_shift() function removes and returns the first element of an array. It also takes a single argument—the array to modify.

<code>$fruits = array("apple", "banana", "orange");
$firstFruit = array_shift($fruits);
</code>

After execution, $firstFruit holds "apple", and $fruits becomes ["banana", "orange"] .

array_unshift() function

The array_unshift() function adds one or more elements to the beginning of an array. It requires the target array and the element(s) to prepend.

<code>$fruits = array("apple", "banana", "orange");
array_unshift($fruits, "watermelon");
</code>

After execution, $fruits becomes ["watermelon", "apple", "banana", "orange"] .

array_reverse() function

The array_reverse() function returns a new array with the order of elements reversed. It takes the array to reverse as its sole argument.

<code>$fruits = array("apple", "banana", "orange");
$reversedFruits = array_reverse($fruits);
</code>

After execution, $reversedFruits is ["orange", "banana", "apple"] .

Summary

This article covered several common PHP array functions— array_push() , array_pop() , array_shift() , array_unshift() , and array_reverse() —demonstrating how they can be used to efficiently manipulate arrays, thereby improving code quality and development productivity.

ProgrammingPHPData Structuresarray-functions
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.