PHP错误与转换对象数组 [英] PHP bug with converting object to arrays
问题描述
我有这个问题早,得出的结论是在5.2.5中的错误。那么,它仍然打破了5.2.6,至少对我来说:
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";
}
任何方法来解决,它已经从一个stdClass的投后阵列?
Any ways to "fix" the array after it has been cast from a stdClass?
推荐答案
肯定似乎是一个错误给我(PHP 5.2.6)。
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));
它的报道在这个bug报告但标记为假... 说的文档:
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;
这篇关于PHP错误与转换对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!