关于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后,键值已不再是原始的键值。
示例代码如下:





































声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
copyright © 2008-2019 亿联网络 版权所有 备案号:粤ICP备14031511号-2