php 带有 NULL 键的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18247412/
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-25 17:14:13  来源:igfitidea点击:

Arrays with NULL keys

phparraysnull

提问by FaddishWorm

PHP trivia here.

PHP琐事在这里。

If we declare an array like this:

如果我们像这样声明一个数组:

<?php $arr = [ 'foo' => 'bar', NULL => 'hello' ]; ?>

We can access it like this

我们可以像这样访问它

print $arr[NULL];

This will print hello. Why is this useful, relevant or necessary? Is it a PHP bug or a feature?

这将打印hello. 为什么这是有用的、相关的或必要的?它是 PHP 错误还是功能?

My only idea was that you could declare array with the NULL key being equal to an error message to explain to anyone who uses a NULL key that their key is null:

我唯一的想法是,您可以使用 NULL 键声明数组等于一条错误消息,以向使用 NULL 键的任何人解释他们的键为空:

$arr[NULL] = 'Warning you have used a null key, did you mean to?';

Has anyone found this useful? Seems to be something to cause more harm than good.

有没有人发现这有用?似乎是弊大于利的事情。

回答by Salman A

Quote from the manual:

手册中的引用:

Null will be cast to the empty string, i.e. the key null will actually be stored under "".

Null 将被转换为空字符串,即键 null 将实际存储在 "" 下。

Additional details about how keys are cast are as follows:

有关如何转换键的其他详细信息如下:

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 decimal integers, unless the number is preceded by a + sign, 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 将实际存储在 "" 下。
  • 数组和对象不能用作键。这样做会导致警告:非法偏移类型。

As for this being useful or necessary, this is debatable. You are asked to use integer or string keys and you have been warned about implicit key casting.

至于这是否有用或必要,这是有争议的。您被要求使用整数或字符串键,并且您已被警告隐式键转换。

回答by mikewasmike

I have found the possibility to have Null keys useful, when accessing db, using a class, which can use one of the column values as the key to the returned array. Some column values could be null.

我发现在访问 db 时,使用类可以使用 Null 键,该类可以使用列值之一作为返回数组的键。某些列值可能为空。

回答by DMCoding

Given a form API populated by an associative php array, I have found it useful to use a null-keyed value to indicate the NULL value in a database. E.g.:

给定一个由关联 php 数组填充的表单 API,我发现使用空键值来指示数据库中的 NULL 值很有用。例如:

$this->addElement('select', 'foreignkey_id', array(
  'label'=>'Add a Poll',
  'multiOptions' => array(
      null => 'No value selected',
      '0' => 'Element with ID0 selected'
  )
));

In this case, null maps to a NULL (default) value in the database column foreignkey_id, and 0 to a row with key 0.

在这种情况下,null 映射到数据库列外键_id 中的 NULL(默认)值,0 映射到键为 0 的行。

Outside this particular usecase however, I tend to think DCoder's comment is authoritative here...

然而,在这个特定的用例之外,我倾向于认为 DCoder 的评论在这里是权威的......