How to mass delete array elements in php?

Possible duplicate:
remove key range in array

I have an array of $ test, it contains 1000 elements with a random key between 1 and 10000, I want to disable array elements of a certain range of keys. for example, I want to disable elements if the key value is from 500 to 600. Now I use the foreach loop for this. Any other php shortcut for this?

+6
source share
2 answers

Original link

remove key range in 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) 
+3
source

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); 
+4
source

Source: https://habr.com/ru/post/925815/


All Articles