PHP array questions about numeric key names

Source: Internet
Author: User
Tags array numeric key range string access

The following is a summary of the PHP array numeric key names:

The length of the key can only be within the length of the int, and will appear overwritten after the int range.

When you access a value in the range of a key name of int, PHP forces the numeric key name to be converted to the INT value type

When the number key name is longer than 19 digits, it becomes 0


The string or numeric type is the same when the key name is normal length

$i = 126545165;
$arr [' 126545165 '] = ' abc ';
$arr [126545165] = ' UiO ';
Var_dump ($arr);
Echo '
'; Var_dump (Isset ($arr [$i]));




When the length exceeds the integral type, the key name is confused <喎?http: www.2cto.com="" kf="" ware="" vc="" "="" target="_blank" class="keylink"> vcd4kpha+phbyzsbjbgfzcz0= "Brush:java;" > $i = 12312312312312; $arr [' 1000000000147483649 '] = ' abc '; $arr [1000000000147483649] = ' UiO '; Var_dump ($arr); Echo '
'; Var_dump (Isset ($arr [$i]));





When the length exceeds 20 digits, the key name becomes 0

$i = 123123123123123123123123123123;
Var_dump ($i);
Echo '
'; $arr [123123123123123123123123123123] = ' abc '; $arr [Strval (123123123123123123123123123123)] = ' abc '; Var_dump ($arr); Echo '
'; Var_dump (Isset ($arr [$i])); Echo '
'; Var_dump (Isset ($arr [Strval ($i)])); Echo '
'; Var_dump (Array_keys ($arr));




The variable is accessed directly as a key name, and the result is different

$i = 123123123123123;
Var_dump ($i);
Echo '
'; $arr [$i] = ' abc '; $arr [Strval ($i)] = ' abc '; Var_dump ($arr); Echo '
'; Var_dump (Isset ($arr [$i])); Echo '
'; Var_dump (Isset ($arr [Strval ($i)])); Echo '
'; Var_dump (Array_keys ($arr));


From several of the above tests, see:

If the key name is a number and the range is within an int, the string or int does not affect access

If the length is greater than int, it is automatically converted to float, the conversion for access occurs in all sorts of confusion, even directly into 0, so it is best to convert to string type


$i = 123123123123123123123123123123;
$j = ' 123123123123123123123123123123 ';
$arr 1[strval ($i)] = ' abc ';
$arr 2[$j] = ' abc ';
Var_dump ($arr 1);
Echo '
'; Var_dump ($arr 2);




So in the dynamic operation of the PHP array, if it is not possible to determine whether the key name will appear number or length is greater than int, then unify the key name Strval into a string to operate the most secure



Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.