php php数组键中允许的字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10696067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:51:07  来源:igfitidea点击:

Characters allowed in php array keys?

phparrayskey

提问by cgwebprojects

I have some php array keys that are populated with a lot of weird characters.

我有一些 php 数组键,其中填充了很多奇怪的字符。

Is this allowed? Are there any constraints to what I cannot use?

这是允许的吗?我不能使用的东西有什么限制吗?

回答by Corbin

According to the manual:

根据手册

The key can either be an integer or a string. The value can be of any type.

Additionally the following key casts will occur:

  • Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
  • Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
  • Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
  • Null will be cast to the empty string, i.e. the key null will actually be stored under "".
  • Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

键可以是整数或字符串。该值可以是任何类型。

此外,将发生以下关键转换:

  • 包含有效整数的字符串将被强制转换为整数类型。例如,键“8”实际上将存储在 8 下。另一方面,“08”不会被强制转换,因为它不是一个有效的十进制整数。
  • 浮点数也被转换为整数,这意味着小数部分将被截断。例如,密钥 8.7 实际上将存储在 8 下。
  • 布尔值也被转换为整数,即键 true 实际存储在 1 下,键 false 存储在 0 下。
  • Null 将被强制转换为空字符串,即键 null 将实际存储在 "" 下。
  • 数组和对象不能用作键。这样做会导致警告:非法偏移类型。

The manual again:

再次手册:

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.

字符串是一系列字符,其中一个字符与一个字节相同。这意味着 PHP 仅支持 256 个字符集,因此不提供本机 Unicode 支持。查看字符串类型的详细信息。

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).

简而言之,任何字符串都可以是键。一个字符串可以包含任何二进制数据(最多 2GB)。因此,键可以是任何二进制数据(因为字符串可以是任何二进制数据)。

Some random (valid) abuse of array keys:

一些随机(有效)滥用数组键:

$w = array(chr(0) => 'null byte?', chr(rand(0, 255)) => 'random byte?');
var_dump($w);

回答by Matthew

The key must be a string or an integer. There are some casts that take place, but I think the manual does a good job of explaining:

键必须是字符串或整数。发生了一些演员表,但我认为手册很好地解释了:

The key can either be an integer or a string. The value can be of any type.

Additionally the following key casts will occur:

  • Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
  • Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
  • Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
  • Null will be cast to the empty string, i.e. the key null will actually be stored under "".
  • Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

键可以是整数或字符串。该值可以是任何类型。

此外,将发生以下关键转换:

  • 包含有效整数的字符串将被强制转换为整数类型。例如,键“8”实际上将存储在 8 下。另一方面,“08”不会被强制转换,因为它不是一个有效的十进制整数。
  • 浮点数也被转换为整数,这意味着小数部分将被截断。例如,密钥 8.7 实际上将存储在 8 下。
  • 布尔值也被转换为整数,即键 true 实际存储在 1 下,键 false 存储在 0 下。
  • Null 将被强制转换为空字符串,即键 null 将实际存储在 "" 下。
  • 数组和对象不能用作键。这样做会导致警告:非法偏移类型。

回答by Rob

I found this answer looking for more information on a problem I had. I was using strings with UTF-8 characters in them, which would not work as keys to an array I had.

我发现这个答案是为了寻找有关我遇到的问题的更多信息。我使用的字符串中包含 UTF-8 字符,它不能作为我拥有的数组的键。

Something like

就像是

$str = "R&D - Solution";
$arr = array( "R&D - Solution" => "Research" );
echo $arr[$str];  // did not work

The (not big or clever) solution for me was to do this..

对我来说(不大也不聪明)的解决方案是这样做..

$str = md5("R&D - Solution");
$arr = array( md5("R&D - Solution") => "Research" );
echo $arr[$str];  // works!

回答by deceze

PHP array keys can be integers or strings. PHP strings are byte arrays, meaning sequences of bytes. There are no other types of strings and PHP doesn't otherwise impose any special restrictions on array key strings. In other words: as long as it's a string, anything goes.

PHP 数组键可以是整数或字符串。PHP 字符串是字节数组,意思是字节序列。没有其他类型的字符串,而且 PHP 不会对数组键字符串施加任何特殊限制。换句话说:只要它是一个字符串,任何事情都会发生。

回答by Marc B

Anything you can stuff into a PHP string can be used as an array key. There's no limit on the characters allowed.

任何可以填充到 PHP 字符串中的东西都可以用作数组键。允许的字符数没有限制。

$a = array();

$x = 'long string of random garage';
echo $a[$x]; // this is ok

$x = array();
echo $a[$x]; // not ok

回答by GordonM

I've personally not had any problems with unusual characters in array keys. What is and isn't legal isn't well documented, other than to say that the key must be a scalar. Your best bet is to just try it and see.

我个人对数组键中的异常字符没有任何问题。除了说密钥必须是标量之外,什么是合法的,什么是不合法的并没有很好的记录。最好的办法就是尝试一下看看。

回答by globalSchmidt

If complex keys are causing an "undefined index" error, you may simply have a "trim" problem.

如果复杂键导致“未定义索引”错误,您可能只是“修剪”问题。

I was going nuts because a complex key was spitting out the "undefined index" error and I thought maybe it was a syntax violation. The array key causing the error was built from a field from a MySQL database query that I was converting into a key and using in a new array. The key looked like this: pl_1DNKoiJKwotCqAycickBVhTyand here's how the code was constructed.

我快疯了,因为一个复杂的键正在吐出“未定义的索引”错误,我想这可能是语法违规。导致错误的数组键是从 MySQL 数据库查询的字段构建的,我将其转换为键并在新数组中使用。关键看起来像这样:pl_1DNKoiJKwotCqAycickBVhTy这是代码的构造方式。

//new array created from database query
$new_array[$dbquery['fieldname']] = {some value};

//key value found in field of second array
$keyval = $array_two['fieldname'];

//this produced the "undefined index" error
echo $new_array[$keyval];

when, in fact, the $keyvaland $dbquery['fieldname']appeared to be a perfect match (visually verified by echoing both to the browser). The mystery was solved by simply using trimin the second statement like this: $keyval = trim($array_two['fieldname']);Once 'trimmed', php no longer complained.

事实上,$keyval$dbquery['fieldname']似乎是完美匹配(通过向浏览器回显两者进行视觉验证)。通过简单地trim在第二个语句中使用,这个谜就解决了:$keyval = trim($array_two['fieldname']);一旦“修剪”,php不再抱怨。

Hoping this saves some others from some frustrating moments...

希望这能让其他人免于一些令人沮丧的时刻......

回答by VeRJiL

In addition to all the answers as they are true: You can use PSRsthat they are some kind of rules between best programmers for having a nice and standard coding style.

除了所有的答案都是正确的:你可以使用PSR,它们是最好的程序员之间的某种规则,用于拥有良好和标准的编码风格。

回答by Fab

Encode the php page in ANSI "é" will be able for use (Cinéma wont show up as Cin??ma). In Notepad++ just use the menu Encode=>Convert ANSI and save

用ANSI“é”编码php页面就可以使用了(Cinéma不会显示为Cin??ma)。在 Notepad++ 中,只需使用菜单 Encode=>Convert ANSI 并保存