6

Possible Duplicate:
unset range of keys in an array

I have an array $test, it contains 1000s of element with random key between 1 and 10000, I want to unset array elements of particular key range. eg i wanna unset elements if the key value between 500 and 600. Now i am using foreach loop to do this. Any other php shortcut to do this?

Community
  • 1
  • 1
EbinPaulose
  • 1,129
  • 3
  • 14
  • 26

2 Answers2

4

How about this (untested, hand-written)

function unsetRange($arr,$from,$to)
{
    for($i=$from;$i<=$to;$i++)
        unset($arr[$i]);
}

// Unset elements from 500 to 600
unsetRange($myArr,500,100);
Jeff
  • 12,085
  • 12
  • 82
  • 152
  • @Mihai Iorga - I provided the code he was asking for: `I want to unset array elements of particular key range.` - Generating the numbers is out of the scope of this question/answer. (atleast IMO) – Jeff Sep 20 '12 at 06:49
3

Original link

unset range of keys in an array

You can try array_slice

$return = array_slice($original, 0, 60)

then

$return = $return+array_slice($original, 70)

or

array_splice

$return = array_splice($original, 60, 10)
Community
  • 1
  • 1
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100