参见英文答案 >
php – how to remove all elements of an array after one specified3个
是否可以删除索引后的所有数组元素?
是否可以删除索引后的所有数组元素?
$myArrayInit = array(1=>red,30=>orange,25=>velvet,45=>pink);
现在一些“神奇”
$myArray = delIndex(30,$myArrayInit);
要得到
$myArray = array(1=>red,30=>orange);
由于$myArray中的键不是连续的,我没有看到array_slice()的机会
请注意:钥匙必须保留!我只知道偏移钥匙!!
不使用循环.
<?PHP $myArrayInit = array(1=>'red',30=>'orange',25=>'velvet',45=>'pink'); //<-- Your actual array $offsetKey=25; //<--- The offset you need to grab //Lets do the code.... $n=array_keys($myArrayInit); //<---- Grab all the keys of your actual array and put in another array $count=array_search($offsetKey,$n); //<--- Returns the position of the offset from this array using search $new_arr=array_slice($myArrayInit,$count+1,true);//<--- Slice it with the 0 index as start and position+1 as the length parameter. print_r($new_arr);
输出:
Array ( [1] => red [30] => orange [25] => velvet )
原文地址:https://www.jb51.cc/php/133293.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。