为什么在php数组中null键值要更改为1 ?

时间:2022-04-01 01:45:56

Following is the code I am testing -

下面是我正在测试的代码—

<?php

error_reporting(E_ALL);

$myarr = array(NULL => "swapnesh", TRUE => 1, 4 => "swap", "swap" => 4, TRUE => NULL, NULL => TRUE );
echo "<pre>";
//var_dump($myarr);
print_r($myarr);
echo count($myarr);

This code outputs -

这段代码输出,

Array
(
    [] => 1
    [1] => 
    [4] => swap
    [swap] => 4
)
4

Concerns/queries regarding the code -

关于代码的关注/查询

  1. Why NULL as a key change value to 1 (at first index) I can consider a scenario when someone may suggest since NULL as a key is used twice so value overwritten but I checked it with FALSE so it must output 0 but no value output in this case.
  2. 为什么NULL作为键值更改为1(第一个索引)我可以考虑这样一种情况:由于NULL作为键被使用了两次,所以value被覆盖,但是我用FALSE检查它,所以它必须输出0,但在这种情况下没有值输出。
  3. At second value 1 is blank however it was supposed to be 1.
  4. 第二个值1是空的,但是它应该是1。
  5. Count is 4 what I was thinking of either 5 or 6 but for 4 I am not sure how it is as last two values skipped.
  6. Count是4,我想的是5或6,但对于4,我不确定最后两个值被忽略了。

Search involved before asking -

在询问之前,先搜索一下

On php net I checked the doc and found this is something related but cant figured out much in my case. Link - http://php.net/manual/en/language.types.array.php

在php网络上,我检查了文档,发现这是相关的,但在我的案例中却找不到。链接- http://php.net/manual/en/language.types.array.php

EDIT For the 3rd point I believe TRUE & NULL is used twice so ie its outputting 4 however let me know if it is exactly the case or not.

编辑第三点,我认为TRUE & NULL被使用了两次,所以它的输出是4,但是让我知道它是否正确。

3 个解决方案

#1


3  

You are using print_r, it makes your output readable so:

您正在使用print_r,它使您的输出可读,因此:

  1. NULL key is overwritten from "swapnesh" to true, printable representation of true is 1
  2. 空键从“swapnesh”覆盖到true, true的可打印表示为1
  3. NULL and false does not produce output in printable format.
  4. NULL和false不会生成可打印格式的输出。
  5. Count is 4 because you are overwriting 2 keys.
  6. 计数是4,因为你覆盖了2个键。

To get better output of variables, use var_dump

要获得更好的变量输出,请使用var_dump

#2


1  

Why NULL as a key change value to 1 (at first index) I can consider a scenario when someone may suggest since NULL as a key is used twice so value overwritten but I checked it with FALSE so it must output 0 but no value output in this case.

为什么NULL作为键值更改为1(第一个索引)我可以考虑这样一种情况:由于NULL作为键被使用了两次,所以value被覆盖,但是我用FALSE检查它,所以它必须输出0,但在这种情况下没有值输出。

PHP prints the boolean FALSE as an empty string. Typecast it into an int to get '0'.

PHP将boolean FALSE打印为空字符串。将它输入一个int数以得到'0'。

At second value 1 is blank however it was supposed to be 1.

第二个值1是空的,但是它应该是1。

TRUE is used as a key twice also, so its value is getting overwritten to NULL, which prints as empty string.

TRUE也被用作两个键,因此它的值被重写为NULL,这将打印为空字符串。

Count is 4 what I was thinking of either 5 or 6 but for 4 I am not sure how it is as last two values skipped.

Count是4,我想的是5或6,但对于4,我不确定最后两个值被忽略了。

Your count is lower because your last two values in the array are overwriting existing values in the array.

计数较低,因为数组中的最后两个值覆盖了数组中的现有值。

#3


0  

null and true are not valid keys. Keys can be either strings or integers. null and true are cast to an empty string and 1 respectively, which means you have several empty string/1 keys which overwrite each other.

null和true都不是有效的键。键可以是字符串也可以是整数。null和true分别被转换为空字符串和1,这意味着您有几个空字符串/1键,它们相互覆盖。

If you want automatically indexed keys, leave the key off:

如果您想要自动索引键,请关闭键:

$myarr = array("swapnesh", 1, 4 => "swap", "swap" => 4, null, true);

#1


3  

You are using print_r, it makes your output readable so:

您正在使用print_r,它使您的输出可读,因此:

  1. NULL key is overwritten from "swapnesh" to true, printable representation of true is 1
  2. 空键从“swapnesh”覆盖到true, true的可打印表示为1
  3. NULL and false does not produce output in printable format.
  4. NULL和false不会生成可打印格式的输出。
  5. Count is 4 because you are overwriting 2 keys.
  6. 计数是4,因为你覆盖了2个键。

To get better output of variables, use var_dump

要获得更好的变量输出,请使用var_dump

#2


1  

Why NULL as a key change value to 1 (at first index) I can consider a scenario when someone may suggest since NULL as a key is used twice so value overwritten but I checked it with FALSE so it must output 0 but no value output in this case.

为什么NULL作为键值更改为1(第一个索引)我可以考虑这样一种情况:由于NULL作为键被使用了两次,所以value被覆盖,但是我用FALSE检查它,所以它必须输出0,但在这种情况下没有值输出。

PHP prints the boolean FALSE as an empty string. Typecast it into an int to get '0'.

PHP将boolean FALSE打印为空字符串。将它输入一个int数以得到'0'。

At second value 1 is blank however it was supposed to be 1.

第二个值1是空的,但是它应该是1。

TRUE is used as a key twice also, so its value is getting overwritten to NULL, which prints as empty string.

TRUE也被用作两个键,因此它的值被重写为NULL,这将打印为空字符串。

Count is 4 what I was thinking of either 5 or 6 but for 4 I am not sure how it is as last two values skipped.

Count是4,我想的是5或6,但对于4,我不确定最后两个值被忽略了。

Your count is lower because your last two values in the array are overwriting existing values in the array.

计数较低,因为数组中的最后两个值覆盖了数组中的现有值。

#3


0  

null and true are not valid keys. Keys can be either strings or integers. null and true are cast to an empty string and 1 respectively, which means you have several empty string/1 keys which overwrite each other.

null和true都不是有效的键。键可以是字符串也可以是整数。null和true分别被转换为空字符串和1,这意味着您有几个空字符串/1键,它们相互覆盖。

If you want automatically indexed keys, leave the key off:

如果您想要自动索引键,请关闭键:

$myarr = array("swapnesh", 1, 4 => "swap", "swap" => 4, null, true);