← Back to PHP Course | Chapter 7: Arrays | Lesson 13 of 13

PHP Array Manipulation

Array manipulation is adding, removing, and cutting up lists, like adding a card to the top of a deck or slicing a piece off. You can push, pop, and splice items as you please.
Syntax
php
array_push($array, $value);      // add to the end
array_pop($array);               // remove from the end
array_unshift($array, $value);   // add to the start
array_shift($array);             // remove from the start
$part = array_slice($array, offset, length);

Adding and Removing from Ends

array_push() and array_pop() add and remove elements from the end of an array respectively, giving you a simple way to use a PHP array as a stack when you need last-in-first-out behavior.

Example: Adding and Removing from Ends

php
<?php
// Declare `$stack` as an array: `[1, 2]`
$stack = [1, 2];
// Call `array_push($stack, 3)`
array_push($stack, 3);
// Call `array_pop($stack)`
array_pop($stack);
// Print a human-readable dump of `$stack`
print_r($stack);
?>

Adding and Removing from Beginning

array_shift() and array_unshift() do the same at the beginning of an array instead of the end, but note they also reindex all the existing numeric keys, which can be an expensive operation on large arrays.

Example: Adding and Removing from Beginning

php
<?php
// Declare `$queue` as an array: `[2, 3]`
$queue = [2, 3];
// Call `array_unshift($queue, 1)`
array_unshift($queue, 1);
// Call `array_shift($queue)`
array_shift($queue);
// Print a human-readable dump of `$queue`
print_r($queue);
?>

Slicing Arrays

array_slice() extracts a portion of an array without modifying the original, while array_splice() removes and optionally replaces a portion in place — mixing these two up is a common source of unexpected side effects.

Example: Slicing Arrays

php
<?php
$arr = [1, 2, 3, 4, 5];
$slice = array_slice($arr, 1, 2);
print_r($slice);
print_r($arr); // unchanged
?>

Splicing Arrays

array_merge() combines two or more arrays into one, with later arrays overwriting earlier ones on matching string keys but simply appending and renumbering on matching numeric keys — a distinction worth testing carefully.

Example: Splicing Arrays

php
<?php
// Declare `$arr1` as an array: `["a" => 1, "b" => 2]`
$arr1 = ["a" => 1, "b" => 2];
// Declare `$arr2` as an array: `["b" => 3, "c" => 4]`
$arr2 = ["b" => 3, "c" => 4];
// Print a human-readable dump of `array_merge($arr1, $arr2)`
print_r(array_merge($arr1, $arr2));
?>

Removing Duplicate Elements

unset($array[$key]) removes a single element by key without reindexing the remaining numeric keys, leaving a gap — if you need a clean, renumbered list afterward, follow it with array_values().

Example: Removing Duplicate Elements

php
<?php
// Declare `$arr` as an array: `[10, 20, 30]`
$arr = [10, 20, 30];
// Remove `$arr[1]`
unset($arr[1]);
// Print a human-readable dump of `$arr`
print_r($arr);
// Print a human-readable dump of `array_values($arr)`
print_r(array_values($arr));
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting array_shift or array_pop to return the new array, when they return the removed element.
  2. Forgetting that array_slice returns a new array and does not modify the original.
  3. Confusing array_splice (changes the array in place) with array_slice (does not).
Chapter Summary
  • Arrays store several values and can be indexed, associative, or multidimensional.
  • You can create, access, update, add, and remove array items.
  • Array functions handle sorting, searching, and manipulation.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.