Skip to content
← Back to Snippets
Code

Remove First Element from Array with array_shift()

Removes the first value from an indexed array with array_shift() and returns the removed value with the reindexed remainder.

Purpose

Removes the first value from an indexed array with array_shift() and returns the removed value with the reindexed remainder.

Snippet details

ContextArrayLevelPracticalCopy-and-paste statusMarked safe after review.

Categories

  • Forms and Validation

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Remove First Element from Array with array_shift().
 *
 * Purpose:
 * Removes the first value from an indexed array.
 *
 * @param array $launch_order Launch order labels.
 * @return array First removed value and remaining order.
 */
function ogSnippetArrayShift(array $launch_order): array {
	$order = array();

	foreach ($launch_order as $entry) {
		if (is_scalar($entry) === true) {
			$entry = trim((string) $entry);

			if ($entry !== '') {
				$order[] = $entry;
			}
		}
	}

	$first_removed = '';

	if (count($order) > 0) {
		$first_removed = array_shift($order);
	}

	return array(
		'removed' => $first_removed,
		'remaining' => $order,
		'remaining_count' => count($order)
	);
}

$shift_report = ogSnippetArrayShift(array('Red Five', 'Gold Leader', 'Blue Squadron'));

echo 'First launched: '.$shift_report['removed'];