将对象转换为数组的 PHP 错误

发布于 2024-07-09 13:00:10 字数 385 浏览 5 评论 0原文

我之前有过这个问题,最后得出的结论是这是 5.2.5 中的一个错误。 好吧,它在 5.2.6 中仍然被破坏,至少对我来说:

请让我知道它是否被破坏或对您有用:

$obj = new stdClass();
$obj->{"foo"} = "bar";
$obj->{"0"} = "zero";
$arr = (array)$obj;

//foo -- bar
//0 --    {error: undefined index}
foreach ($arr as $key=>$value){
    echo "$key -- " . $arr[$key] . "\n";
}

在从 stdClass 转换数组后,有什么方法可以“修复”数组吗?

I had this question earlier and it was concluded it was a bug in 5.2.5. Well, it's still broken in 5.2.6, at least for me:

Please let me know if it is broken or works for you:

$obj = new stdClass();
$obj->{"foo"} = "bar";
$obj->{"0"} = "zero";
$arr = (array)$obj;

//foo -- bar
//0 --    {error: undefined index}
foreach ($arr as $key=>$value){
    echo "$key -- " . $arr[$key] . "\n";
}

Any ways to "fix" the array after it has been cast from a stdClass?

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

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

发布评论

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

评论(3

无声无音无过去 2024-07-16 13:00:10

对我来说绝对像是一个错误(PHP 5.2.6)。

您可以像这样修复数组:

$arr = array_combine(array_keys($arr), array_values($arr));

此错误报告中已报告该问题,但是标记为伪造... 文档 说:

键是成员变量
名称,但有一些值得注意的例外:
整数属性无法访问;

Definitely seems like a bug to me (PHP 5.2.6).

You can fix the array like this:

$arr = array_combine(array_keys($arr), array_values($arr));

It's been reported in this bug report but marked as bogus... the documentation says:

The keys are the member variable
names, with a few notable exceptions:
integer properties are unaccessible;

謸气贵蔟 2024-07-16 13:00:10

一些实验表明 php 自己的函数不会持续这种模糊性。

function noopa( $a ){ return $a; }
$arr = array_map('noopa', $arr ); 
$arr[0]; # no error! 

实际上,这只是创建了数组的副本,并且修复发生在复制过程中。

最终,它是一个全面的设计失败,尝试以您认为它在具有混合数字和字符串键的数组上工作的方式使用 array_merge 吗?

所有编号的元素都会被复制,有些元素会被重新编号,即使有些元素恰好是字符串封装的数字,因此,有数十种 array_merge 的自制实现来解决这个问题。

当 php 尝试克隆 Perl 并失败时,他们没有掌握数组和哈希是不同概念的概念,而是将它们组合在一起形成一个通用的伞。 干得好!

对于他们的下一个技巧,他们设法打破名称空间分隔符,因为由于某种原因其他语言没有遇到过一些技术问题。

A bit of experimentation shows phps own functions don't persist this fubarity.

function noopa( $a ){ return $a; }
$arr = array_map('noopa', $arr ); 
$arr[0]; # no error! 

This in effect, just creates a copy of the array, and the fix occurs during the copy.

Ultimately, its a design failure across the board, try using array_merge in the way you think it works on an array with mixed numeric and string keys?

All numbered elements get copied and some get re-numbered, even if some merely happen to be string-encapsulated-numbers, and as a result, there are dozens of homebrew implementations of array_merge just to solve this problem.

Back when php tried to make a clone of perl and failed, they didn't grasp the concept of arrays and hashes being distinct concepts, an instead globbed them together into one universal umbrella. Good going!.

For their next trick, they manage to break namespace delimiters because of some technical problem that no other language has for some reason encountered.

凡尘雨 2024-07-16 13:00:10

谢谢 RoBorg ..我也刚刚发现:)

这是另一个解决方案,不确定它是否更快:

unserialize(serialize($arr));

Thanks RoBorg.. I just discovered that as well :)

Here's another solution, not sure if it's faster or not:

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