douzhanshen0657 2014-01-21 16:17
浏览 337

使用array_slice()获取前几个值

I'd like to use an array_slice to get every value in an array iterated over, except for the current one, the one before it, and all the others until the end of the array.

So for example, say I have 5 elements:

[1, 2, 3, 4, 5]

I want a way for a condition to fire on 3, cut it out, and then also cut out 4 and 5. I have this, but I don't think it's correct:

$items = array_slice($items, $itemcount - 1);
  • 写回答

1条回答 默认 最新

  • dss89001 2014-01-21 16:25
    关注

    array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.

    So if you want to get the 3,4,5 your offset should be 2, because the array key ( starts from 0 ex: 0,1,2,3,4). i didn't use length in this one because i want to get it till the end (5)

    $items = array_slice($items, 2);
    

    If you want to grag 1,2 it should be like this. ( i use length 2 because i want to get the first 2 keys)

    $items = array_slice($items, 0, 2);
    

    Example

    评论

报告相同问题?

悬赏问题

  • ¥20 MIT控制器能控制不稳定系统吗
  • ¥15 公司代码X对业务伙伴X无效,处理方法?
  • ¥15 微信内链接跳转到浏览器打开怎么实现
  • ¥15 三角波可以直接加施密特电路整形到矩形波吗实物
  • ¥15 html,php,在使用html请求php文件时发生了错误,无法请求到php文件读取数据库并用javascript进行数据显示,刷新
  • ¥15 touchsocket udp组播
  • ¥20 MAC怎么安装Silverlight 插件?以及安装了怎么启用
  • ¥15 VS2012中查询语句无法填入解析,数值传不进去
  • ¥15 gis系统开发出现命名空间“ESRI.ArcGIS”中不存在类型或命名空间名“Analyst3D”报错
  • ¥15 怎么让ai定时给我发信息 c#或者python