Home > Backend Development > PHP Tutorial > Analysis of precautions for modifying array key with array_unshift() in PHP, array_unshiftkey_PHP tutorial

Analysis of precautions for modifying array key with array_unshift() in PHP, array_unshiftkey_PHP tutorial

WBOY
Release: 2016-07-12 08:52:06
Original
1107 people have browsed it

Analysis of precautions for modifying array key by array_unshift() in php, array_unshiftkey

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>";

Copy after login

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 )

Copy after login

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1127886.htmlTechArticleAnalysis of precautions for modifying array key by array_unshift() in php, array_unshiftkey This article analyzes the modification of array by array_unshift() in php Key notes. Share it with everyone for your reference, with...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template