Verification: a143cc29221c9be0

Php array delete first element

Variable names

Make sure that your variable names are descriptive of what they contain. In particular $max1 and $max2 use a poor naming style as it is unclear what you expect them to contain.

Whitespace

Make sure to keep whitespace around your operators, even in a for-loop. This increases readability, and thus decreases the chance for bugs.

Unneeded bookkeeping

You currently use $offset--; but do not use the result. Remove code you do not use.

Your current code only works once, because you keep the keys. Considering the following code:

$offset = 3;
$elements = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];

$max1 = $offset;
$max2 = count($elements);
for ($i=0; ($i int(3) [4]=> int(4) [5]=> int(5) [6]=> int(6) [7]=> int(7) [8]=> int(8) [9]=> int(9) [10]=> int(10) [11]=> int(11) [12]=> int(12) [13]=> int(13) }

You would expect it to remove the first 3 elements twice, but because the keys are not reset, the second iteration of your code will try to remove key 0, 1 and 2, while the first key is 3.

Your current code modifies an array in-place while keeping the keys. Modifying arrays while looping over them is aweful in any language, as you need to be mindful that conditions in loops may be pre-calculated, and element pointers may skip elements.

Since you use a for-loop, I can assume you use an array with numeric keys, which makes it odd that you would want to keep the current keys.

Alternative creating a new array

In php you can use array_slice to obtain a new array with only a slice of the original array. This will reset the keys of the array.

$elements = array_slice($elements, $offset);

No loop required.

Alternative deleting elements

You can use array_splice to modify an array in-place by deleting a consecutive chunk of array elements. It returns the deleted elements and the original array has a few less elements. This will also reset the keys.

array_splice($elements, 0, $offset);

Again, no loop required.

Alternative with a loop

You can also use a loop and use array_shift to remove the first element of the array repeatedly. For numeric keys the keys are reset, while literal keys are left untouched.

for ($i = 0; $i 

Remove an Element from an Array by its Key Using unset()

The easiest way to remove or delete a single element from an array in PHP is to use the unset() function. This function is used to unset a given variable. In other words, you can use it to destroy any variable in your code.

To remove an array element by its key, provide the unset() function with the key of the element that you want to delete. In case of numeric arrays, this key will be the index of the element. Here is an example:

PHP

$num_array = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$asc_array = ["first" => "Amanda", "second" => "John",
              "third" => "Kelly", "fourth" => "Megan",
              "fifth" => "Henry"];

unset($num_array[2]);
print_r($num_array);
/* Output —
Array
(
    [0] => Amanda
    [1] => John
    [3] => Megan
    [4] => Henry
) */

unset($asc_array["third"]);
print_r($asc_array);
/* Output —
Array
(
    [first] => Amanda
    [second] => John
    [fourth] => Megan
    [fifth] => Henry
) */

You should note that the keys of both the arrays remain unchanged after deleting the element from the arrays. If you want to reindex the keys after removing the element so that there is no gap, you can use the array_values() function. This function returns an indexed array of all the values in the supplied array.

Remember that array_values() will also change the keys of associative array to numerical indices. The following example will clear up how reindexing an array after removing an element works.

PHP

$num_array = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$asc_array = ["first" => "Amanda", "second" => "John",
              "third" => "Kelly", "fourth" => "Megan",
              "fifth" => "Henry"];

unset($num_array[2]);
$reindexed = array_values($num_array);
print_r($reindexed);
/* Output —
Array
(
    [0] => Amanda
    [1] => John
    [2] => Megan
    [3] => Henry
) */

unset($asc_array["third"]);
$reindexed = array_values($asc_array);
print_r($reindexed);
/* Output —
Array
(
    [0] => Amanda
    [1] => John
    [2] => Megan
    [3] => Henry
) */

Remove an Element from an Array by its Value Using unset()

The only problem with the above solution is that you actually need to know the key to delete an element. Let’s say you only know the value of the element that you want to delete and have no idea about its key. In such cases, you can first use the array_search() function in PHP to get the key for a given value and then use unset() to delete the element.

PHP

$num_array = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$asc_array = ["first" => "Amanda", "second" => "John",
              "third" => "Kelly", "fourth" => "Megan",
              "fifth" => "Henry"];

$key = array_search("John", $num_array);
unset($num_array[$key]);
print_r($num_array);
/* Output —
Array
(
    [0] => Amanda
    [2] => Kelly
    [3] => Megan
    [4] => Henry
) */

$key = array_search("John", $asc_array);
unset($asc_array[$key]);
print_r($asc_array);
/* Output —
Array
(
    [first] => Amanda
    [third] => Kelly
    [fourth] => Megan
    [fifth] => Henry
) */

As you saw in the above example, you can use array_search() and unset() together to remove or delete an array element by its value.

Using array_splice() to Delete Multiple Consecutive Elements from an Array

The array_splice() function in PHP is used to remove a portion of an array and replace it with something else. However, providing the replacement is optional and you can use this function to remove a portion of the array in one go. Here is an example:

PHP

$num_array = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$asc_array = ["first" => "Amanda", "second" => "John",
              "third" => "Kelly", "fourth" => "Megan",
              "fifth" => "Henry"];
$fruits = ["Apple", "Mango", "Banana", "Lichi", "Pear", "Grapes", "Guava", "Papaya", "Orange"];

array_splice($num_array, 2, 1);
print_r($num_array);
/* Output —
Array
(
    [0] => Amanda
    [1] => John
    [2] => Megan
    [3] => Henry
) */

array_splice($asc_array, 2, 1);
print_r($asc_array);
/* Output —
Array
(
    [first] => Amanda
    [second] => John
    [fourth] => Megan
    [fifth] => Henry
) */

array_splice($fruits, 3, 4);
print_r($fruits);
/* Output —
Array
(
    [0] => Apple
    [1] => Mango
    [2] => Banana
    [3] => Papaya
    [4] => Orange
) */

Besides removing multiple elements at once, another advantage of this method is that the numeric keys were automatically re-indexed. At the same time, the keys of the associative array remained unchanged.

One important point that you should remember is that you have to pass the offset as second parameter and not the key. That’s why we did not pass the key third as a value of second array_splice() parameter when deleting elements from associative array. The third parameter is used to specify the number of elements starting from the offset that we want to remove. You can set it to 1 to remove a single element or set it to some other higher value to remove multiple elements.

Using array_diff() to Delete Multiple Elements by Their Value

While array_splice() can remove multiple elements from any at once, it does so without comparing either keys or value. Another problem with array_splice() is that it will delete elements after the offset in a sequential manner so you won’t be able to use it to delete non-consecutive elements.

What if you want to delete array elements by their value and those elements are not placed sequentially? The array_diff() function in PHP proves very useful in this situation. This function is used to remove all the elements in the first array that are also present in second array. In other words, you can use this method to remove non-consecutive array elements by their value with ease. You should note that the keys in second array do not matter. Only the values need to match for removal of elements.

PHP

$num_array = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$asc_array = ["first" => "Amanda", "second" => "John",
              "third" => "Kelly", "fourth" => "Megan",
              "fifth" => "Henry"];

$num_diff = ["Amanda", "Megan"];
$asc_diff = ["bogus_a" => "Amanda", "bogus_b" => "Megan"];

$num_array = array_diff($num_array, $num_diff);
print_r($num_array);
/* Output —
Array
(
    [1] => John
    [2] => Kelly
    [4] => Henry
) */

$asc_array = array_diff($asc_array, $asc_diff);
print_r($asc_array);
/* Output —
Array
(
    [second] => John
    [third] => Kelly
    [fifth] => Henry
) */

As you can see in the above example, you can use this technique to remove multiple elements by their value from numeric as well as associative arrays. One thing that I would like to point out is that the keys in the second array don’t have to match the first array for deletion to be successful.

Using array_diff_key() to Delete Multiple Array Elements by Their Keys

Just like you use array_diff() to delete multiple non-consecutive elements from an array by their value, you can use the array_diff_key() function to delete multiple non-consecutive array elements by their key. This function is used to remove all the elements from first array whose key is also present in the second array. You should note the the values in the second array don’t matter. Only the keys need to match for the removal of elements. Here is an example:

PHP

$num_array = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$num_array_b = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$asc_array = ["first" => "Amanda", "second" => "John",
              "third" => "Kelly", "fourth" => "Megan",
              "fifth" => "Henry"];

$num_diff = [0, 3];
$num_diff_b = [0 => "Bogus", 3 => "Bogus"];
$asc_diff = ["first" => "Bogus", "third" => "Bogus"];

// $num_diff is used as [0 => 0, 1 => 3];
$num_array = array_diff_key($num_array, $num_diff);
print_r($num_array);
/* Output —
Array
(
    [2] => Kelly
    [3] => Megan
    [4] => Henry
) */

$num_array_b = array_diff_key($num_array_b, $num_diff_b);
print_r($num_array_b);
/* Output —
Array
(
    [1] => John
    [2] => Kelly
    [4] => Henry
) */

$asc_array = array_diff_key($asc_array, $asc_diff);
print_r($asc_array);
/* Output —
Array
(
    [second] => John
    [fourth] => Megan
    [fifth] => Henry
) */

In the above example, the output of the first array_diff_key() operation might come as a surprise to some of you. At first it looks like we are passing the numeric keys of elements to remove like we are supposed to do. However, array_diff_key() turns the simple array we passed into an associative array with the element 0 at index (or key) 0 and the element 3 at index (or key) 1. So the keys that you are passing for removal effectively become 0 and 1 instead of 0 and 3 like you intended to. We remedy the situation by passing bogus values for keys 0 and 3 in the second case and get the expected output. This did not become a problem in the previous section because we were matching keys and not values.