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

发布于 2024-07-07 15:35:20 字数 446 浏览 8 评论 0原文

我有一个看起来是空字符串的键,但是使用 unset($array[""]); 不会删除键/值对。 我没有看到另一个函数可以执行我想要的操作,所以我猜它比调用函数更复杂。

print_r 上元素的行是 [] =>; 1,这向我表明该键是空字符串。

使用 var_export,元素被列为 '' =>; 1..

使用 var_dump,元素被列为 [""]=>int(1)

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

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.

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

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

Using var_dump, the element is listed as [""]=>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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

萝莉病 2024-07-14 15:35:20

尝试 unset($array[null]);

如果这不起作用,请通过 var_exportvar_dump 而不是 打印数组print_r,因为这可以让您看到密钥的类型。 使用 var_export 以 PHP 语法查看数据。

var_export($array);

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

Try 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. Use var_export to see the data in PHP syntax.

var_export($array);

Note that var_export does not work with recursive structures.

下雨或天晴 2024-07-14 15:35:20

尝试过:

$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/>';

得到:

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

你也应该分析密钥来自哪里......

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

勿忘初心 2024-07-14 15:35:20

我的猜测是它不是一个空字符串。 尝试以下操作看看您会得到什么:

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

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";
}
自由如风 2024-07-14 15:35:20

尝试使用 var_dump 而不是 print_r。 这可能会让您更好地了解关键是什么。

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

メ斷腸人バ 2024-07-14 15:35:20

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

<?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 );

我得到以下输出

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

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

您确定您没有使用数组的副本吗? 如果您从函数内部调用了 unset(),那么您可能就是这样。

这是在 PHP 5.2.0 上测试的

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.

This was tested on PHP 5.2.0

指尖上得阳光 2024-07-14 15:35:20

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

我正在寻找的是这样的:

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

请同时发布两行 var_export 的完整输出。

我正在寻找这样的东西:

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

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.

I'm looking for something like this:

array (
  '' => 1,
)
array (
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文