248

I have some troubles with an array. I have one array that I want to modify like below. I want to remove element (elements) of it by index and then re-index array. Is it possible?

$foo = array(

    'whatever', // [0]
    'foo', // [1]
    'bar' // [2]

);

$foo2 = array(

    'foo', // [0], before [1]
    'bar' // [1], before [2]

);

8 Answers 8

538
unset($foo[0]); // remove item at index 0
$foo2 = array_values($foo); // 'reindex' array
Sign up to request clarification or add additional context in comments.

2 Comments

This is better than deceze's solution if several items needs to be removed at once - if the indexes of those items are collected beforehand. Once you use array_splice on the first item, you lose the indexes of the rest items.
It may be worth noting that you can unset multiple variables/array indexes in a single unset call unset($foo[0], $foo[3], $bar[1]);
52
array_splice($array, 0, 1);

http://php.net/manual/en/function.array-splice.php

1 Comment

Beware of using this in a loop because it puts the array indices of the loop and of the actual array out of sync.
30

You better use array_shift(). That will return the first element of the array, remove it from the array and re-index the array. All in one efficient method.

2 Comments

This is fine when you want to retrieve and then remove the first array element, reindexing the array in the process. Its counterpart function array_pop() will retrieve and remove the last array element if you need that. But, neither function can be made to act on an element in the middle of the array.
While this is orthogonally related, the question is explicitely about any element of an array, not just the first.
13
array_splice($array, array_search(array_value, $array), 1);

2 Comments

where array_value will be 'whatever'
The explanation belongs in the question, not posted as a comment under the question.
11

2020 Benchmark in PHP 7.4

For these who are not satisfied with current answers, I did a little benchmark script, anyone can run from CLI.

We are going to compare two solutions:

unset() with array_values() VS array_splice().

<?php

echo 'php v' . phpversion() . "\n";

$itemsOne = [];
$itemsTwo = [];

// populate items array with 100k random strings
for ($i = 0; $i < 100000; $i++) {
    $itemsOne[] = $itemsTwo[] = sha1(uniqid(true));
}

$start = microtime(true);

for ($i = 0; $i < 10000; $i++) {
    unset($itemsOne[$i]);
    $itemsOne = array_values($itemsOne);
}

$end = microtime(true);

echo 'unset & array_values: ' . ($end - $start) . 's' . "\n";

$start = microtime(true);

for ($i = 0; $i < 10000; $i++) {
    array_splice($itemsTwo, $i, 1);
}

$end = microtime(true);

echo 'array_splice: ' . ($end - $start) . 's' . "\n"; 

As you can see the idea is simple:

  • Create two arrays both with the same 100k items (randomly generated strings)
  • Remove 10k first items from first array using unset() and array_values() to reindex
  • Remove 10k first items from second array using array_splice()
  • Measure time for both methods

Output of the script above on my Dell Latitude i7-6600U 2.60GHz x 4 and 15.5GiB RAM:

php v7.4.8
unset & array_values: 29.089932918549s
array_splice: 17.94264793396s

Verdict: array_splice is almost twice more performant than unset and array_values.

So: array_splice is the winner!

1 Comment

A third test would be to unset() all 10k, then run array_values() to re-index one time instead of on every loop. That is ~5% faster than array_splice() for me, using your script.
9
Unset($array[0]); 

Sort($array); 

I don't know why this is being downvoted, but if anyone has bothered to try it, you will notice that it works. Using sort on an array reassigns the keys of the the array. The only drawback is it sorts the values. Since the keys will obviously be reassigned, even with array_values, it does not matter is the values are being sorted or not.

6 Comments

even if the keys will be reassigned, in the correct answer, the initial order will be kept.
"I want to remove element (elements) of it by index and then re-index array. Is it possible?" @s3v3n The OP never asked for maintaining initial order. I simply provided a solution that works and does what is stated.
@s3v3n It is not a grudge. I just wish to know why. Obviously to improve in later answers. I agree that there are better methods, but is there a reason this answer is so bad? Is it performance? etc? Same reason I am on SO, to learn as well as teach.
Under normal circumstances, it is very likely that the programmer will need those values in exactly the same order as the input is. Let's take as an example some rows from the database that should be displayed in a table; or a chat application where the replies should be in the exactly same order. The number of examples can be infinite, the point is that in most cases the order needs to be the same or at least the programmer would expect the same order of the elements in the array.
OK. The database example just made perfect sense to me. Thanks for taking the time out.
|
5

Try with:

$foo2 = array_slice($foo, 1);

1 Comment

This only works for the first element and not an arbitrary one.
0

After some time I will just copy all array elements (excluding these unwanted) to new array

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.