24

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.

1

5 Answers 5

52
foreach($arr as $k => $v) {
    if(key($v) > 5) {
        unset($arr[$k]);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

facepalm... i totally forgot about the $k => $v syntax for a moment, my memory is getting worse and worse.
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 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.
17

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]);
    }
}

2 Comments

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, thank you, I missed the fact that array of arrays is given, I've corrected the example. Also added note about foreach loop.
2

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

foreach($arr as $k => $v) {
    if(key($v) > 5) {
        unset($arr[$k]);
    }
}

1 Comment

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).
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?

5 Comments

foreach loops in PHP iterate on a copy of the array provided by default (unless it is explicitly passed by reference).
reference vs copy was not my point, nor relevant. it is bad form to mutate an array during iteration.
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.
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.
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.
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]);
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.