简体   繁体   English

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

[英]How do you remove a value that has an empty key from an associative array in PHP?

I have a key that appears to be an empty string, however using unset($array[""]); 我有一个似乎是空字符串的键,但是使用了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. 我看不到另一个函数可以满足我的要求,因此我猜测仅调用一个函数会更加复杂。

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[""]); , unset($array['']); unset($array['']); , and unset($array[null]); unset($array[null]); with no luck. 没有运气。

Try unset($array[null]); 尝试unset($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. 如果这不起作用,请通过var_exportvar_dump而不是print_r来打印数组,因为这使您可以查看键的类型。 Use var_export to see the data in PHP syntax. 使用var_export查看PHP语法中的数据。

var_export($array);

Note that var_export does not work with recursive structures. 请注意,var_export不适用于递归结构。

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... 您还应该分析密钥的来源...

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";
}

Try using var_dump instead of print_r . 尝试使用var_dump而不是print_r This may give you a better idea of what exactly the key is. 这可以使您更好地了解密钥的确切含义。

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? 看看如何将“空”数组键类型转换为空字符串?

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上进行了测试

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 (
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM