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.

$fruits = array("apple", "banana", "orange");
array_push($fruits, "watermelon");

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.

$fruits = array("apple", "banana", "orange");
$lastFruit = array_pop($fruits);

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.

$fruits = array("apple", "banana", "orange");
$firstFruit = array_shift($fruits);

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.

$fruits = array("apple", "banana", "orange");
array_unshift($fruits, "watermelon");

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.

$fruits = array("apple", "banana", "orange");
$reversedFruits = array_reverse($fruits);

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPData 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

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.