daGrevis
daGrevis

Reputation: 21333

How to Remove Array Element and Then Re-Index Array?

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]

);

Upvotes: 245

Views: 225806

Answers (8)

Michal - wereda-net
Michal - wereda-net

Reputation: 959

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

Upvotes: 0

barell
barell

Reputation: 1489

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!

Upvotes: 11

user1092222
user1092222

Reputation: 163

array_splice($array, array_search(array_value, $array), 1);

Upvotes: 13

Rene
Rene

Reputation: 309

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.

Upvotes: 30

frostymarvelous
frostymarvelous

Reputation: 2805

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.

Upvotes: 9

hsz
hsz

Reputation: 152216

Try with:

$foo2 = array_slice($foo, 1);

Upvotes: 5

deceze
deceze

Reputation: 522024

array_splice($array, 0, 1);

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

Upvotes: 52

xzyfer
xzyfer

Reputation: 14135

unset($foo[0]); // remove item at index 0
$foo2 = array_values($foo); // 'reindex' array

Upvotes: 536

Related Questions