PHP array_slice()空长度导致空数组 [英] PHP array_slice() null length results in empty array

查看:50
本文介绍了PHP array_slice()空长度导致空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,我想使用array_slice删除数组的第一个元素.这是命令:

I have an array where I want to use array_slice to remove the first element of the array. Here's the command:

$myArray = array_slice($myArray, 1);

现在,array_slice具有第4个参数,当设置为true时,它将保留阵列键而不是将其重置.我确实需要将此选项设置为true.

Now, array_slice has a 4th argument, when set to true, it preserves the array keys instead of resetting them. I do need this option set to true.

第三个参数用于指定结果数组的长度.如果希望将数组切成数组的末尾而不是指定长度,则应该忽略此参数.

The 3rd argument is to specify the length of the resulting array. You are supposed to leave this argument out if you want the array to be sliced to the end of the array instead of specifying a length.

所以我尝试了这个:

$myArray = array_slice($myArray, 1, NULL, true);

这将导致一个空数组.我究竟做错了什么?还有另一种方法可以在不将长度参数设置为NULL的情况下省去"长度参数吗?因为将其设置为NULL似乎完全清空了我的数组.

And this results in an empty array. What am I doing wrong? Is there another way to "leave out" the length argument without setting it to NULL? Because setting it to NULL seems to empty my array completely.

此外,我的解决方法是:

Also, my workaround is to do this:

$myArray = array_slice($myArray, 1, count($myArray)-1, true);

但是看来我不必这样做...

but it doesn't seem like I should have to do that...

更新

这似乎是PHP 5.1.6的错误

This appears to be a bug with PHP 5.1.6

推荐答案

我刚刚测试了这段代码:

I just tested this code:

$myArray = array(
'test1' => 'test1',
'test2' => 'test2',
'test3' => 'test3',
'test4' => 'test4',
);

var_dump(array_slice($myArray, 1, null, true));

仅显示test2,test3和test4对我来说效果很好.

And it works fine for me by only showing test2, test3, and test4.

这是在5.2.13上运行的.尝试升级PHP(我也知道,但是这是一个开发箱).

This was run on 5.2.13. Try upgrading PHP (I know I should too but this is a dev box).

这篇关于PHP array_slice()空长度导致空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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