如何从 PHP 关联数组中删除具有空键的值?
我有一个看起来是空字符串的键,但是使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试
unset($array[null]);
如果这不起作用,请通过
var_export
或var_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
orvar_dump
instead ofprint_r
, since this allows you to see the type of the key. Usevar_export
to see the data in PHP syntax.var_export($array);
Note that var_export does not work with recursive structures.
尝试过:
得到:
你也应该分析密钥来自哪里......
Tried:
Got:
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:
尝试使用
var_dump
而不是print_r
。 这可能会让您更好地了解关键是什么。Try using
var_dump
instead ofprint_r
. This may give you a better idea of what exactly the key is.不知道该告诉你什么。 运行此脚本
我得到以下输出
看看“null”数组键是如何类型转换为空字符串的?
您确定您没有使用数组的副本吗? 如果您从函数内部调用了 unset(),那么您可能就是这样。
这是在 PHP 5.2.0 上测试的
Not sure what to tell you. Running this script
I get the following output
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
请在该行之前和之后发布您用于删除元素的代码以及检查器代码。
我正在寻找的是这样的:
请同时发布两行
var_export
的完整输出。我正在寻找这样的东西:
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:
Please also post the complete output of both
var_export
lines.I'm looking for something like this: