This article analyzes the precautions for modifying array key by array_unshift() in php. Share it with everyone for your reference, the details are as follows:
As we all know, array_unshift() is used to add elements to the beginning of the array, but today I suddenly discovered that if the key value of the array is of numeric type (or can be converted to numeric type), array_unshift() will modify all keys The Key of a numeric element is really a trap
Example:
$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 )
You see, the array key value has changed after array_unshift(), the original 111 has become 1, what a trap! Everyone needs to pay special attention to this when using array_unshift() in the future!
Supplement: The editor here recommends a PHP formatting and beautifying typesetting tool on this website to help you code typesetting in future PHP programming:
PHP code online formatting and beautification tool: http://tools.jb51.net/code/phpformat
Readers who are interested in more PHP-related content can check out the special topics on this site: "Complete PHP Array Operation Skills", "Summary of PHP Sorting Algorithms", "Summary of PHP Common Traversal Algorithms and Techniques", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "PHP Mathematical Operation Skills Summary", "php Regular Expression Usage Summary", "PHP Operations and Operator Usage Summary", "php String Usage Summary" 》and《Summary of common database operation skills in php》
I hope this article will be helpful to everyone in PHP programming.