Home > php教程 > php手册 > body text

php中array_unshift()修改数组key注意事项分析,array_unshiftkey

WBOY
Release: 2016-06-13 08:39:35
Original
1444 people have browsed it

php中array_unshift()修改数组key注意事项分析,array_unshiftkey

本文实例分析了php中array_unshift()修改数组key注意事项。分享给大家供大家参考,具体如下:

众所周知,array_unshift()用来在数组的开头添加元素,但今天突然发现如果数组的键值是数字类型的(或能转成数字类型的),array_unshift()会修改所有键为数字的元素的Key,真是坑啊

实例:

$a=array(111=>"dddddddddddd","112"=>array("one"=>"orange","two"=>"hhhhh"), "113"=>array("one"=>"orange","two"=>"hhhhh"),"oooo"=>"jjjjj");
print_r($a);echo "</br>";
array_unshift($a, "aaaaaaaaa");
print_r($a);echo "</br>";

Copy after login

输出结果:

Array ( [111] => dddddddddddd [112] => Array ( [one] => orange [two] => hhhhh ) [113] => Array ( [one] => orange [two] => hhhhh ) [oooo] => jjjjj )
Array ( [0] => aaaaaaaaa [1] => dddddddddddd [2] => Array ( [one] => orange [two] => hhhhh ) [3] => Array ( [one] => orange [two] => hhhhh ) [oooo] => jjjjj )

Copy after login

看到了吧,array_unshift()后数组键值发生了变化,原来的111变成了1,真是坑哪!以后大家在使用array_unshift()的时候对此需要格外注意了!

补充:小编在这里推荐一款本站的php格式化美化的排版工具帮助大家在以后的PHP程序设计中进行代码排版:

php代码在线格式化美化工具:http://tools.jb51.net/code/phpformat

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数组(Array)操作技巧大全》、《php排序算法总结》、《PHP常用遍历算法与技巧总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php正则表达式用法总结》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!