In php, array_unshift () is used to analyze precautions for modifying the array key. array_unshiftkey
This article analyzes the precautions for changing the array key of array_unshift () in php. We will share this with you for your reference. The details are as follows:
As we all know, array_unshift () is used to add elements at the beginning of the array, but today we suddenly find that if the array's key value is of the numeric type (or can be converted to the numeric type), array_unshift () it's a pitfall to change the keys of all the elements whose keys are numbers.
Instance:
$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>";
Output result:
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 )
As you can see, the array key value changes after array_unshift (), and the original 111 is changed to 1! In the future, you will need to pay special attention to this when using array_unshift!
Supplement: The editor recommends a php formatting and formatting typographical tool on this site to help you typeset code in future PHP programming:
Php code online formatting and beautification tools:Http://tools.jb51.net/code/phpformat