php数组键中允许的字符? [英] Characters allowed in php array keys?
问题描述
我有一些 php 数组键,其中填充了很多奇怪的字符.
I have some php array keys that are populated with a lot of weird characters.
这允许吗?我不能使用的东西有什么限制吗?
Is this allowed? Are there any constraints to what I cannot use?
推荐答案
根据 手册:
键可以是整数或字符串.值可以是任何类型.
The key can either be an integer or a string. The value can be of any type.
此外还会发生以下键转换:
Additionally the following key casts will occur:
- 包含有效整数的字符串将被转换为整数类型.例如.键8"实际上将存储在 8 下.另一方面,08"不会被强制转换,因为它不是有效的十进制整数.
- 浮点数也被转换为整数,这意味着小数部分将被截断.例如.密钥 8.7 实际上将存储在 8 下.
- 布尔值也被转换为整数,即键 true 实际存储在 1 下,键 false 存储在 0 下.
- Null 将被转换为空字符串,即键 null 将实际存储在 "" 下.
- 数组和对象不能用作键.这样做会导致警告:非法偏移类型.
手册再次:
字符串是一系列字符,其中一个字符与一个字节相同.这意味着 PHP 仅支持 256 个字符集,因此不提供本机 Unicode 支持.查看字符串类型的详细信息.
A string is series of characters, where a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support. See details of the string type.
简而言之,任何字符串都可以是键.一个字符串可以包含任何二进制数据(最多 2GB).因此,键可以是任何二进制数据(因为字符串可以是任何二进制数据).
So in short, any string can be a key. And a string can contain any binary data (up to 2GB). Therefore, a key can be any binary data (since a string can be any binary data).
一些随机(有效)滥用数组键:
Some random (valid) abuse of array keys:
$w = array(chr(0) => 'null byte?', chr(rand(0, 255)) => 'random byte?');
var_dump($w);
这篇关于php数组键中允许的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!