PHP在某个键值之前删除数组的一部分 [英] PHP Remove section of array before a certain key value

查看:268
本文介绍了PHP在某个键值之前删除数组的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按特定顺序排列的数组,我想从第一索引到给定键的索引处截取一部分数组。

I have an array that is in a certain order and I want to just cutoff a portion of the array starting from the first index to the index of a given key.

IE ...
如果我有此数组

IE... If i had this array

$array = array("0" => 'blue', "1" => 'red', "2" => 'green', "3" => 'red', "4"=>"purple");

我想剪切键 2之前的数组的第一部分(作为字符串) )被看到。
因此,结束数组将类似于...

I want to cut off the first part of the array before the key "2" (as a string) is seen. So the end array would be something like...


2 =>'green'

3 =>'红色'

4 =>'紫色'

"2" => 'green'
"3" => 'red'
"4"=>'purple'

谢谢,
Ian

Thanks, Ian

推荐答案

对于您的情况,您可以使用

For your case you can use

print_r(array_slice($array, 2, count($array),true));

编辑:对于已编辑的问题

For edited question

$cloneArray = $array;
foreach($array as $key => $value){
  if($key == $givenInex)
     break;

  unset($cloneArray[$key]);
} 

然后使用$ cloneArray

Then use $cloneArray

这篇关于PHP在某个键值之前删除数组的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆