本文實(shí)例分析了php中array_unshift()修改數(shù)組key注意事項(xiàng)。分享給大家供大家參考,具體如下:
眾所周知,array_unshift()用來在數(shù)組的開頭添加元素,但今天突然發(fā)現(xiàn)如果數(shù)組的鍵值是數(shù)字類型的(或能轉(zhuǎn)成數(shù)字類型的),array_unshift()會(huì)修改所有鍵為數(shù)字的元素的Key,真是坑啊
實(shí)例:
$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>";
輸出結(jié)果:
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 )
看到了吧,array_unshift()后數(shù)組鍵值發(fā)生了變化,原來的111變成了1,真是坑哪!以后大家在使用array_unshift()的時(shí)候?qū)Υ诵枰裢庾⒁饬耍?/P>
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。