23

So here is my code:

<?php

$arr = array(array(2 => 5),
             array(3 => 4),
             array(7 => 10));

foreach ($arr as $v) {
    $k = key($v);
    if ($k > 5) {
        // unset this element from $arr array
    }
}

print_r($arr);

// now I would like to get the array without array(7 => 10) member

As you can see, I start with an array of single key => value arrays, I loop through this array and get a key of the current element (which is a single item array).

I need to unset elements of the array with key higher than 5, how could I do that? I might also need to remove elements with value less than 50 or any other condition. Basically I need to be able to get a key of the current array item which is itself an array with a single item.

Richard Knop
  • 81,041
  • 149
  • 392
  • 552
  • 1
    possible duplicate of [How do you remove an array element in a foreach loop?](http://stackoverflow.com/questions/1949259/how-do-you-remove-an-array-element-in-a-foreach-loop) – 7hi4g0 Jan 29 '14 at 06:48

5 Answers5

52
foreach($arr as $k => $v) {
    if(key($v) > 5) {
        unset($arr[$k]);
    }
}
sasa
  • 2,443
  • 5
  • 23
  • 35
  • 1
    facepalm... i totally forgot about the $k => $v syntax for a moment, my memory is getting worse and worse. – Richard Knop May 17 '10 at 20:28
  • 1
    What is the difference in using $k and key($v), aren't they same? Just asking because every example below also uses key($v) don't know why when I think $k can be used directly. – Haider Oct 10 '14 at 19:02
  • 2
    @Haider You're just misunderstanding what `key($v)` is being used for. It gets the key of the sub-array that is `$v`, not any keys of the array being iterated over. For the first element, `$k == 0`, `$v == array(2 => 5)`, and `key($v) == 2`. – cincodenada Apr 21 '15 at 00:09
16

It is safe in PHP to remove elements from an array while iterating over it using foreach loop:

foreach ($arr as $key => $value) {
    if (key($value) > 5) {
        unset($arr[$key]);
    }
}
Alexander Konstantinov
  • 5,406
  • 1
  • 26
  • 31
  • 2
    Note that this doesn't actually do what the OP wants (but your point is mostly correct - it is safe to do so in the context of a `foreach` loop; other forms of iteration are not always add/remove-safe). – Amber May 17 '10 at 20:20
  • @Amber, thank you, I missed the fact that array of arrays is given, I've corrected the example. Also added note about `foreach` loop. – Alexander Konstantinov May 17 '10 at 20:29
2

Use key() to get the first key from the sub-array.

foreach($arr as $k => $v) {
    if(key($v) > 5) {
        unset($arr[$k]);
    }
}
Amber
  • 507,862
  • 82
  • 626
  • 550
  • Um, no, I actually wasn't aware of your answer when I edited my response. The fact that the two answers are the same is more due to the fact that the basicness of the task at hand leads to it being done the same way by many people (the inside of the loop is essentially the same as before I edited it; the outside is pretty much the standard for `foreach` loops). – Amber May 17 '10 at 20:43
0

To unset array element we Used unset() and php function like below:

foreach($array as $key=>$value)
{
   if(key($value) > 5) 
   {
      unset($array[$key]);
   }
}
Rabesh Lal Shrestha
  • 304
  • 1
  • 6
  • 18
0

It's not really safe to add or delete from a collection while iterating through it. How about adding the elements you want to a second array, then dumping the original?

Robert McBean
  • 67
  • 1
  • 4
  • 2
    `foreach` loops in PHP iterate on a copy of the array provided by default (unless it is explicitly passed by reference). – Amber May 17 '10 at 20:18
  • 1
    reference vs copy was not my point, nor relevant. it is bad form to mutate an array during iteration. – Robert McBean May 17 '10 at 20:25
  • Except that you're *not* mutating the array you're iterating over, because the array you're iterating over is a *copy* of the original. It is not bad form to mutate a *different* array while iterating, and thus it is not bad form to mutate the original array from a non-referenced-passed foreach loop, because the original array is a different array. – Amber May 17 '10 at 20:45
  • this may be technically true, but semantically the developer thinks he is removing something while iterating which I think is bad design, especially when a keyword like foreach suggests a read-only traversal. It will work, but that's the kind of practice that makes code badly maintainable. – Sean Patrick Floyd May 17 '10 at 21:35
  • 1
    How does `foreach` suggest read-only? It suggests that you're doing something **for each** item in the thing passed, which is exactly what you're doing. That makes no statements about whether any of those items will still be present in the original array afterwards. – Amber May 17 '10 at 22:08