与null键数组 [英] Arrays with NULL keys

查看:121
本文介绍了与null键数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP琐事这里。

如果我们声明数组是这样的:

If we declare an array like this:

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

我们可以访问它像这样

print $arr[NULL];

这将打印你好。为什么这是有用的,相关的或有必要吗?它是一个PHP错误或功能?

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

我唯一的想法是,你可以用null键等于一个错误消息来解释的人谁使用了空关键,他们的关键是空声明数组:

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:

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

推荐答案

手动:

空将转换为空字符串,即关键的空实际上会
  下的被保存。

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

有关密钥如何投其他详细信息如下:

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:


      含有效整数
  • 字符串将转换为整数类型。
      例如。键8实际上将8下被存储在另一方面
      08不会被投射,因为它不是一个有效的小数整数

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

浮筒也投射到整数,这意味着小数部分
  将被截断。例如。关键8.7实际上将被保存8下。

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铸造为整数,也即真正的关键实际上是
  存储在1和0下的关键假的。

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.

至于这是非常有用的或必要的,这是值得商榷的。你被要求使用整数或字符串键,你已经被警告有关隐式按键铸造。

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.

这篇关于与null键数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆