0

This question has been asked a thousand times, but each question I find talks about associative arrays where one can delete (unset) an item by using they key as an identifier. But how do you do this if you have a simple array, and no key-value pairs?

Input code

$bananas = array('big_banana', 'small_banana', 'ripe_banana', 'yellow_banana', 'green_banana', 'brown_banana', 'peeled_banana');

foreach ($bananas as $banana) {
    // do stuff
    // remove current item
}

In Perl I would work with for and indices instead, but I am not sure that's the (safest?) way to go - even though from what I hear PHP is less strict in these things.

Note that after foreach has run, I expected var_dump($bananas) to return an empty array (or null, but preferably an empty array).

Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
  • 2
    `array_shift` is the best option here. – Murad Hasan Apr 28 '16 at 08:04
  • @FrayneKonok But doesn't `array_shift` re-index the whole array? Isn't that a problem when you're looping? – Bram Vanroy Apr 28 '16 at 08:05
  • do you need to re-index??? – Murad Hasan Apr 28 '16 at 08:06
  • All arrays have indexes, only because you don't write them explicit doesn't mean the array doesn't have them. So here you just have a 0-based indexed array. – Rizier123 Apr 28 '16 at 08:07
  • @FrayneKonok No I don't, but from what I [read](http://php.net/manual/en/function.array-slice.php) slicing will re-index the array, and I am wondering if the array is reindexed on the first iteration, won't the second iteration start on `ripe_banana` instead of `small_banana`? – Bram Vanroy Apr 28 '16 at 08:08
  • @Rizier123 I am aware of that, but if an array is re-indexed, doesn't that result in inconsistencies? **Iteration 1** on `big_banana`, ends with shifting that item and *re-indexing*, so `small_banana` becomes 0. But in **iteration 2** the loop might go searching for an index 1 (because 0 has already been done), but index 1 is now `ripe_banana` and not the expected `small_banana`. – Bram Vanroy Apr 28 '16 at 08:11
  • Just unset it, see: http://stackoverflow.com/q/369602/3933332 – Rizier123 Apr 28 '16 at 08:11
  • I'm not an expert on PHP internals but the C source code for `array_shift` says `re-index like it did before`: https://github.com/php/php-src/blob/6499162ff0d8aa6e862d3e3cdd2288b87636b8a1/ext/standard/array.c#L2708 – martin Apr 28 '16 at 08:13
  • why you need to unset the value in the foreach loop???? – Murad Hasan Apr 28 '16 at 08:13
  • @BramVanroy, you have to say something, All we are waiting for your comment. What you really want?? – Murad Hasan Apr 28 '16 at 12:09

6 Answers6

0

it still has the indexes

 foreach ($bananas as $key => $banana) {
        // do stuff
        unset($bananas[$key]);
    }
kejsu
  • 384
  • 2
  • 5
0

1st method (delete by value comparison):

$bananas = array('big_banana', 'small_banana', 'ripe_banana', 'yellow_banana', 'green_banana', 'brown_banana', 'peeled_banana');

foreach ($bananas as $key=>$banana) {
    if($banana=='big_banana')
        unset($bananas[$key]);
}

2nd method (delete by key):

$bananas = array('big_banana', 'small_banana', 'ripe_banana', 'yellow_banana', 'green_banana', 'brown_banana', 'peeled_banana');
unset($bananas[0]);    //removes the first value
unset($bananas[count($bananas)-1]);    //removes the last value
//unset($bananas[n-1]);    removes the nth value

Finally if you want to reset the keys after deletion process:

$bananas = array_map('array_values', $bananas);

If you want to empty the array completely:

unset($bananas);
$bananas= array();
sariDon
  • 7,782
  • 2
  • 16
  • 27
0
for($i=0; $i<count($bananas); $i++)
{
    //doStuff
    unset($bananas[$i]);
}

This will delete every element after its use so you will eventually end up with an empty array.

If for some reason you need to reindex after deleting you can use array_values

dimlucas
  • 5,040
  • 7
  • 37
  • 54
0

How about a while loop with array_shift?

while (($item = array_shift($bananas)) !== null)
{
    // 
}
Hkan
  • 3,243
  • 2
  • 22
  • 27
0

Your Note: Note that after foreach has run, I expected var_dump($bananas) to return an empty array (or null, but preferably an empty array).

Simply use unset.

foreach ($bananas as $banana) {
    // do stuff
    // remove current item
    unset($bananas[$key]);
}


print_r($bananas);

Result

Array
(
)
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
0

This question is old but I will post my idea using array_slice for new visitors.

while(!empty($bananas)) {
    // ... do something with $bananas[0] like
    echo $bananas[0].'<br>';
    $bananas = array_slice($bananas, 1);
}
Wesley Gonçalves
  • 1,985
  • 2
  • 19
  • 22