关于array_shift
前几天用到array_shift这个函数,发现得到的结果并非是自己所预想的,后来又仔细看了下PHP Manual,发现了其中的原因(注意黑体部分):
array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won"t be touched. If array is empty (or is not an array), NULL will be returned.
我没有得到预期结果的原因正是因为我进行array_shift的数组的键是数字型(numerical)的,进行array_shift后,键值已不再是原始的键值。
示例代码如下:
<?php
$arr = array("name" => "wong", "age" => 24, "sex" => "male");
print_r($arr);
array_shift($arr);
print_r($arr);

$arr = array(45 => "wong", 46 => 24, 47 => "male");
print_r($arr);
array_shift($arr);
print_r($arr);

/*
Output:
Array
(
[name] => wong
[age] => 24
[sex] => male
)
Array
(
[age] => 24
[sex] => male
)
Array
(
[45] => wong
[46] => 24
[47] => male
)
Array
(
[0] => 24
[1] => male
)
*/
?>
array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won"t be touched. If array is empty (or is not an array), NULL will be returned.
我没有得到预期结果的原因正是因为我进行array_shift的数组的键是数字型(numerical)的,进行array_shift后,键值已不再是原始的键值。
示例代码如下:
<?php
$arr = array("name" => "wong", "age" => 24, "sex" => "male");
print_r($arr);
array_shift($arr);
print_r($arr);
$arr = array(45 => "wong", 46 => 24, 47 => "male");
print_r($arr);
array_shift($arr);
print_r($arr);
/*
Output:
Array
(
[name] => wong
[age] => 24
[sex] => male
)
Array
(
[age] => 24
[sex] => male
)
Array
(
[45] => wong
[46] => 24
[47] => male
)
Array
(
[0] => 24
[1] => male
)
*/
?>声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了
copyright © 2008-2019 入门客AI创业平台 版权所有 备案号:湘ICP备2023012770号
