Home  >  Article  >  Backend Development  >  Problems with numeric key names in PHP array_PHP tutorial

Problems with numeric key names in PHP array_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:29:17931browse

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

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

When the key name length is int range, PHP will force the numeric key name to be converted into int numeric type

When the numeric key name is longer than 19 digits, it will become 0


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

$i = 126545165;
$arr['126545165'] = 'abc';
$arr[126545165] = 'uio';
var_dump($arr);
echo &#39;<br>&#39;;
var_dump(isset($arr[$i]));




When the length exceeds an integer, the key name will be confused喎?http://www.Bkjia.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 characters, the key name will become 0

$i = 123123123123123123123123123123;
var_dump($i);
echo &#39;<br>&#39;;
$arr[123123123123123123123123123123] = &#39;abc&#39;;
$arr[strval(123123123123123123123123123123)] = &#39;abc&#39;;
var_dump($arr);
echo &#39;<br>&#39;;
var_dump(isset($arr[$i]));
echo &#39;<br>&#39;;
var_dump(isset($arr[strval($i)]));
echo &#39;<br>&#39;;
var_dump(array_keys($arr));




Accessing variables directly as key names has different results

$i = 123123123123123;
var_dump($i);
echo &#39;<br>&#39;;
$arr[$i] = &#39;abc&#39;;
$arr[strval($i)] = &#39;abc&#39;;
var_dump($arr);
echo &#39;<br>&#39;;
var_dump(isset($arr[$i]));
echo &#39;<br>&#39;;
var_dump(isset($arr[strval($i)]));
echo &#39;<br>&#39;;
var_dump(array_keys($arr));


Judging from the above tests:

If the key name is a number and the range is within int, string or int will not have any impact on access

If the length is greater than int, it will be automatically converted to float. Then conversion and access will cause various confusions, or even directly become 0, so it is best to uniformly convert to string type


$i = 123123123123123123123123123123;
$j = &#39;123123123123123123123123123123&#39;;
$arr1[strval($i)] = &#39;abc&#39;;
$arr2[$j] = &#39;abc&#39;;
var_dump($arr1);
echo &#39;<br>&#39;;
var_dump($arr2);




So when dynamically operating a PHP array, if you are not sure whether the key name will contain numbers or the length is greater than int, it is most secure to uniformly convert the key name strval to a string for operation

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/777563.htmlTechArticleThe following is a summary of PHP array numeric key names: The key name length can only be within the int length range, After exceeding the int range, confusion such as overwriting will occur. The key name length is int range memory...
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