如果数组键不存在,如何将其设置为null?

时间:2021-05-17 23:02:20

For example i have an array:

例如,我有一个数组:

array(
    'a' => 'value',
    'b' => 'value',
    'c',
    'd' => 'value',
    'e' => array(
        'f' => 'value',
        'g',
        array(
            'h' => 'value',
            'i'
        )
    ),
    'k',
    'l' => 'value'
);

I need recursively walk trough it and set key to NULL if it doesn't exists like this:

我需要递归地遍历它并将键设置为NULL,如果它不存在,如下所示:

array(
    'a' => 'value',
    'b' => 'value',
    NULL => 'c',
    'd' => 'value',
    'e' => array(
        'f' => 'value',
        NULL => 'g',
        array(
            'h' => 'value',
            NULL => 'i'
        )
    ),
    NULL => 'k',
    'l' => 'value'
);

UPDATE I need this because i need to encode array in JSON and push to browser. The problem is that the json_encode sets key to 0 if it not exists, but if there is NULL it also remains the same in browser. So when i using this array in JS, i can detect where is the real 0 and where 0 was created because there was no key.

更新我需要这个,因为我需要在JSON中编码数组并推送到浏览器。问题是如果json_encode不存在,则将key设置为0,但如果有NULL,则它在浏览器中也保持不变。所以当我在JS中使用这个数组时,我可以检测到真正的0在哪里以及因为没有键而创建了0。

2 个解决方案

#1


2  

The keys do exist, they are just automatically assigned and are numeric. From Arrays

密钥确实存在,它们只是自动分配并且是数字的。来自阵列

The key is optional. If it is not specified, PHP will use the increment of the largest previously used integer key.

钥匙是可选的。如果未指定,PHP将使用先前使用的最大整数键的增量。

You cannot detect, if the numeric key was assigned explicitly or if the key was just omitted. You can do this only at the time when the array is created or the value is appended to the array. Afterwards, you can only replace numeric keys with some other value.

如果明确指定了数字键或者刚刚省略了键,则无法检测到。您只能在创建数组或将值附加到数组时执行此操作。之后,您只能用其他值替换数字键。

Additionally

If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.

如果数组声明中的多个元素使用相同的键,则只会使用最后一个键,因为所有其他元素都将被覆盖。

This means, you cannot replace missing keys with NULL, but only one of them. If you set multiple keys to NULL, every assignment will delete the previous NULL key/value pair.

这意味着,您不能用NULL替换缺少的键,而只能替换其中一个。如果将多个键设置为NULL,则每个赋值都将删除先前的NULL键/值对。

And finally

Additionally the following key casts will occur:

此外,还会发生以下关键演员:

  • Null will be cast to the empty string, i.e. the key null will actually be stored under "".
  • Null将被强制转换为空字符串,即密钥null实际上将存储在“”下。

#2


0  

Use array_walk_recursive.

array_walk_recursive($array, function (&$value, &$key) {
    $value = NULL; /* for example as you cannot use multiple NULL-keys nor set the key to NULL... */
});

P.s.: For an explanation why you cannot use NULL-keys, see the post of @OlafDietsche

P.s。:为了解释为什么你不能使用NULL键,请参阅@OlafDietsche的帖子

#1


2  

The keys do exist, they are just automatically assigned and are numeric. From Arrays

密钥确实存在,它们只是自动分配并且是数字的。来自阵列

The key is optional. If it is not specified, PHP will use the increment of the largest previously used integer key.

钥匙是可选的。如果未指定,PHP将使用先前使用的最大整数键的增量。

You cannot detect, if the numeric key was assigned explicitly or if the key was just omitted. You can do this only at the time when the array is created or the value is appended to the array. Afterwards, you can only replace numeric keys with some other value.

如果明确指定了数字键或者刚刚省略了键,则无法检测到。您只能在创建数组或将值附加到数组时执行此操作。之后,您只能用其他值替换数字键。

Additionally

If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.

如果数组声明中的多个元素使用相同的键,则只会使用最后一个键,因为所有其他元素都将被覆盖。

This means, you cannot replace missing keys with NULL, but only one of them. If you set multiple keys to NULL, every assignment will delete the previous NULL key/value pair.

这意味着,您不能用NULL替换缺少的键,而只能替换其中一个。如果将多个键设置为NULL,则每个赋值都将删除先前的NULL键/值对。

And finally

Additionally the following key casts will occur:

此外,还会发生以下关键演员:

  • Null will be cast to the empty string, i.e. the key null will actually be stored under "".
  • Null将被强制转换为空字符串,即密钥null实际上将存储在“”下。

#2


0  

Use array_walk_recursive.

array_walk_recursive($array, function (&$value, &$key) {
    $value = NULL; /* for example as you cannot use multiple NULL-keys nor set the key to NULL... */
});

P.s.: For an explanation why you cannot use NULL-keys, see the post of @OlafDietsche

P.s。:为了解释为什么你不能使用NULL键,请参阅@OlafDietsche的帖子