Organizational Research By

Surprising Reserch Topic

unset array element inside a foreach loop using -'php'


unset array element inside a foreach loop  using -'php'

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.
    
asked Sep 7, 2015 by rajesh
0 votes
3 views



Related Hot Questions



Government Jobs Opening


...