Horje
Remove First Element from an Array in PHP

Given an array, the task is to remove the first element from an array in PHP.

Examples:

Input: arr = [1, 2, 3, 4, 5, 6, 7];
Output:   2, 3, 4, 5, 6, 7

Input: arr = [3, 4, 5, 6, 7, 1, 2]
Output:  4, 5, 6, 7, 1, 2

Below are the methods to remove the first element from an array in PHP:

Using array_shift() function

The array_shift() function removes the first element from an array and returns it. This function also re-indexes the array so that the keys start from 0 again.

Example: This example shows the implementation of the above-explained approach.

PHP
<?php

    $arr = array(1, 2, 3, 4, 5, 6);
    $removedElement = array_shift($arr);
    echo "Removed element: " . $removedElement . "\n";

    echo "Updated array: ";
    print_r($arr);
?>

Output
Removed element: 1
Updated array: Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 6
)

Using array_slice() function

The array_slice() function is used to extract a portion of an array. By specifying a start index of 1, you can extract all elements except the first one.

Example: This example shows the implementation of the above-explained approach.

PHP
<?php

    $arr = array(1, 2, 3, 4, 5, 6);

    $arr = array_slice($arr, 1);

    echo "Updated array: ";
    print_r($arr);

?>

Output
Updated array: Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 6
)

Using unset() Function

The unset() function is used to remove the first element from an array. This function takes the array and the index of the element to be removed as arguments.

Example: This example shows the implementation of the above-explained approach.

PHP
<?php

    $arr = array(1, 2, 3, 4, 5, 6);

    unset($arr[0]);
    $arr = array_values($arr); // Re-index the array

    echo "Updated array: ";
    print_r($arr);

?>

Output
Updated array: Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 6
)

Using array_splice() Function

The array_splice() function can remove elements from an array and can also replace them with other elements. By specifying the start index of 0 and a length of 1, you can remove the first element of the array.

Example: This example shows the implementation of the above-explained approach.

PHP
<?php

    $arr = array(1, 2, 3, 4, 5, 6);

    array_splice($arr, 0, 1);

    echo "Updated array: ";
    print_r($arr);

?>

Output
Updated array: Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 6
)

Using array_shift() and array_values() functions

The array_shift() function removes the first element from an array and returns it. However, it also re-indexes the array starting from 0. To maintain the original indexing after removing the first element, you can use array_values().

Example:

PHP
<?php
// Sample array
$arr = [1, 2, 3, 4, 5, 6, 7];

// Remove the first element and re-index the array
array_shift($arr);

// Re-index the array to start from 0
$arr = array_values($arr);

// Output the modified array
print_r($arr);
?>

Output
Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 6
    [5] => 7
)





Reffered: https://www.geeksforgeeks.org


PHP

Related
How to Encode Array in JSON PHP ? How to Encode Array in JSON PHP ?
PHP Exception Handling with Multiple Catch Blocks PHP Exception Handling with Multiple Catch Blocks
Sort an Associative Array by Key in PHP Sort an Associative Array by Key in PHP
Integrating SAP with PHP Integrating SAP with PHP
Extract Substrings Between Brackets in PHP Extract Substrings Between Brackets in PHP

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11