I have the array:
我有阵列:
$array=array('red'=>12,'blue'=>14,'violet'=>88,'green'=>58);
Now I want to remove all values of the array starting from the key 'violet'. So I can get an array equivalent to:
现在我想从键'violet'开始删除数组的所有值。所以我可以得到一个相当于的数组:
$array=array('red'=>12,'blue'=>14);
How can I achieve this goal, without looping all the array? Thanks a lot!
如何在不循环所有阵列的情况下实现这一目标?非常感谢!
2 个解决方案
#1
4
array_slice(), array_search(), array_keys()
array_slice(),array_search(),array_keys()
$array=array('red'=>12,'blue'=>14,'violet'=>88,'green'=>58);
$new_array = array_slice($array,0,array_search("violet",array_keys($array)));
print_r($new_array);
#2
0
Maybe this helps:
也许这有助于:
array_slice($array,0,array_search("violet",array_keys($array)));
#1
4
array_slice(), array_search(), array_keys()
array_slice(),array_search(),array_keys()
$array=array('red'=>12,'blue'=>14,'violet'=>88,'green'=>58);
$new_array = array_slice($array,0,array_search("violet",array_keys($array)));
print_r($new_array);
#2
0
Maybe this helps:
也许这有助于:
array_slice($array,0,array_search("violet",array_keys($array)));