Questions about the number key name in the PHP Array

Source: Internet
Author: User

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

The length of the key name can only be within the int length range. If the value exceeds the int range, confusion such as overwriting will occur.

When the key name length is an int-range memory value, PHP will forcibly convert the number key name to an int numeric type

When the number key name length is greater than 19 digits, it will become 0


When the key name is of the normal length, the string or value type is the same

$i = 126545165;$arr['126545165'] = 'abc';$arr[126545165] = 'uio';var_dump($arr);echo '
';var_dump(isset($arr[$i]));




When the length exceeds the integer type, the key name is messy. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PHByZSBjbGFzcz0 = "brush: java;"> $ I = 12312312312312; $ arr ['000000'] = 'abc'; $ arr [1000000000147483649] = 'uio '; var_dump ($ arr); echo'
'; Var_dump (isset ($ arr [$ I]);





If the length exceeds 20 bits, the key name will be 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 variables are directly accessed as key names, and the results are 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 the above tests:

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

If the length is greater than int, it will be automatically converted to float, and then converted for access in various situations, or even directly changed to 0, so it is best to convert it to the string type in a unified manner.


$i = 123123123123123123123123123123;$j = '123123123123123123123123123123';$arr1[strval($i)] = 'abc';$arr2[$j] = 'abc';var_dump($arr1);echo '
';var_dump($arr2);




Therefore, when dynamically operating the PHP array, if you cannot determine whether the key name contains digits or the length is greater than int, it is the most secure to convert the key name strval to a string.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.