Home>Article>Backend Development> Precautions and example analysis of modifying array key with array_unshift() in php

Precautions and example analysis of modifying array key with array_unshift() in php

墨辰丷
墨辰丷 Original
2018-06-02 11:12:52 2204browse

This article mainly introduces the precautions for modifying the array key with array_unshift() in PHP. The example analyzes the situation where the array_unshift() function automatically converts the key value when the key value is a numeric type. Friends who need it can refer to it

As we all know, array_unshift() is used to add elements to the beginning of the array, but today I suddenly discovered thatIf the key value of the array is of numeric type (or can be converted to numeric type), array_unshift() will modify all The Keyof an element whose key is a number 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 "
"; array_unshift($a, "aaaaaaaaa"); print_r($a);echo "
";

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 )

See it, the array key value changed after array_unshift(), the original 111 became 1, what a trap! Everyone needs to pay special attention to this when using array_unshift() in the future!

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

phpRecognize upside-down pictures taken by flipping the iPhone

PHP implementation login Verification code verification function

PHP algorithm example sharing for converting URLs into short URLs

The above is the detailed content of Precautions and example analysis of modifying array key with array_unshift() in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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