如何从PHP中的关联数组中删除具有空键的值?

时间:2022-08-26 14:11:27

I have a key that appears to be an empty string, however using unset($array[""]); does not remove the key/value pair. I don't see another function that does what I want, so I'm guessing it's more complicated that just calling a function.

我有一个看似空字符串的键,但是使用unset($ array [“”]);不会删除键/值对。我没有看到另外一个功能可以实现我想要的功能,所以我猜测只是调用一个函数会更复杂。

The line for the element on a print_r is [] => 1, which indicates to me that the key is the empty string.

print_r上元素的行是[] => 1,它向我指示键是空字符串。

Using var_export, the element is listed as '' => 1.

使用var_export,该元素列为''=> 1。

Using var_dump, the element is listed as [""]=>int(1).

使用var_dump,该元素列为[“”] => int(1)。

So far, I have tried all of the suggested methods of removal, but none have removed the element. I have tried unset($array[""]);, unset($array['']);, and unset($array[null]); with no luck.

到目前为止,我已经尝试了所有建议的删除方法,但没有删除该元素。我试过unset($ array [“”]);, unset($ array ['']);和unset($ array [null]);没有运气。

6 个解决方案

#1


24  

Try unset($array[null]);

尝试取消设置($ array [null]);

If that doesn't work, print the array via var_export or var_dump instead of print_r, since this allows you to see the type of the key. Use var_export to see the data in PHP syntax.

如果这不起作用,则通过var_export或var_dump而不是print_r打印数组,因为这样可以查看键的类型。使用var_export以PHP语法查看数据。

var_export($array);

var_export($阵列);

Note that var_export does not work with recursive structures.

请注意,var_export不适用于递归结构。

#2


2  

Tried:

尝试:

$someList = Array('A' => 'Foo', 'B' => 'Bar', '' => 'Bah');
print_r($someList);
echo '<br/>';
unset($someList['A']);
print_r($someList);
echo '<br/>';
unset($someList['']);
print_r($someList);
echo '<br/>';

Got:

得到:

Array ( [A] => Foo [B] => Bar [] => Bah )
Array ( [B] => Bar [] => Bah )
Array ( [B] => Bar )

You should analyse where the key come from, too...

您应该分析密钥的来源......

#3


1  

My guess is that it's not an empty string. Try the following to see what you get:

我的猜测是它不是一个空字符串。尝试以下方法,看看你得到了什么:

foreach ($array as $index => $value) {
    echo $index;
    echo ' is ';
    echo gettype($index);
    echo "\n";
}

#4


1  

Try using var_dump instead of print_r. This may give you a better idea of what exactly the key is.

尝试使用var_dump而不是print_r。这可以让您更好地了解密钥究竟是什么。

#5


1  

Not sure what to tell you. Running this script

不知道该告诉你什么。运行此脚本

<?php

$arr = array(
        false   => 1
    ,   true    => 2
    ,   null    => 3
    ,   'test'  => 4
//  ,   ''      => 5
);

print_r( $arr );

foreach ( $arr as $key => $value )
{
    var_dump( $key );
}

unset( $arr[''] );

print_r( $arr );

I get the following output

我得到以下输出

Array
(
    [0] => 1
    [1] => 2
    [] => 3
    [test] => 4
)
int(0)
int(1)
string(0) ""
string(4) "test"
Array
(
    [0] => 1
    [1] => 2
    [test] => 4
)

See how the "null" array key was type converted to an empty string?

看看“null”数组键是如何被类型转换为空字符串的?

Are you sure you are not working with a copy of the array? If you did this call to unset() from inside a function, it's possible that you are.

你确定你没有使用阵列的副本吗?如果您从函数内部调用unset(),则可能是您。

This was tested on PHP 5.2.0

这是在PHP 5.2.0上测试的

#6


0  

Please post the code you use to remove the element as well your checker code before and after that line.

请发布您用于删除元素的代码以及该行之前和之后的检查器代码。

What I'm looking for is something like this:

我正在寻找的是这样的:

var_export($array);
echo "\n";
unset($array[""]);
var_export($array);

Please also post the complete output of both var_export lines.

还请发布两个var_export行的完整输出。

I'm looking for something like this:

我正在寻找这样的东西:

array (
  '' => 1,
)
array (
)

#1


24  

Try unset($array[null]);

尝试取消设置($ array [null]);

If that doesn't work, print the array via var_export or var_dump instead of print_r, since this allows you to see the type of the key. Use var_export to see the data in PHP syntax.

如果这不起作用,则通过var_export或var_dump而不是print_r打印数组,因为这样可以查看键的类型。使用var_export以PHP语法查看数据。

var_export($array);

var_export($阵列);

Note that var_export does not work with recursive structures.

请注意,var_export不适用于递归结构。

#2


2  

Tried:

尝试:

$someList = Array('A' => 'Foo', 'B' => 'Bar', '' => 'Bah');
print_r($someList);
echo '<br/>';
unset($someList['A']);
print_r($someList);
echo '<br/>';
unset($someList['']);
print_r($someList);
echo '<br/>';

Got:

得到:

Array ( [A] => Foo [B] => Bar [] => Bah )
Array ( [B] => Bar [] => Bah )
Array ( [B] => Bar )

You should analyse where the key come from, too...

您应该分析密钥的来源......

#3


1  

My guess is that it's not an empty string. Try the following to see what you get:

我的猜测是它不是一个空字符串。尝试以下方法,看看你得到了什么:

foreach ($array as $index => $value) {
    echo $index;
    echo ' is ';
    echo gettype($index);
    echo "\n";
}

#4


1  

Try using var_dump instead of print_r. This may give you a better idea of what exactly the key is.

尝试使用var_dump而不是print_r。这可以让您更好地了解密钥究竟是什么。

#5


1  

Not sure what to tell you. Running this script

不知道该告诉你什么。运行此脚本

<?php

$arr = array(
        false   => 1
    ,   true    => 2
    ,   null    => 3
    ,   'test'  => 4
//  ,   ''      => 5
);

print_r( $arr );

foreach ( $arr as $key => $value )
{
    var_dump( $key );
}

unset( $arr[''] );

print_r( $arr );

I get the following output

我得到以下输出

Array
(
    [0] => 1
    [1] => 2
    [] => 3
    [test] => 4
)
int(0)
int(1)
string(0) ""
string(4) "test"
Array
(
    [0] => 1
    [1] => 2
    [test] => 4
)

See how the "null" array key was type converted to an empty string?

看看“null”数组键是如何被类型转换为空字符串的?

Are you sure you are not working with a copy of the array? If you did this call to unset() from inside a function, it's possible that you are.

你确定你没有使用阵列的副本吗?如果您从函数内部调用unset(),则可能是您。

This was tested on PHP 5.2.0

这是在PHP 5.2.0上测试的

#6


0  

Please post the code you use to remove the element as well your checker code before and after that line.

请发布您用于删除元素的代码以及该行之前和之后的检查器代码。

What I'm looking for is something like this:

我正在寻找的是这样的:

var_export($array);
echo "\n";
unset($array[""]);
var_export($array);

Please also post the complete output of both var_export lines.

还请发布两个var_export行的完整输出。

I'm looking for something like this:

我正在寻找这样的东西:

array (
  '' => 1,
)
array (
)