stdClass 对象问题
我正在努力使用 PHP 解析以下数据。 API 返回它,我尝试了各种语法。如何以非对象方式返回数据?或者,使用 stdClass 调用数据的语法是什么?
我可以将其转换为一个基于数据的数组,甚至两个吗?当谈到基于对象的数据集时,我迷失了方向。
stdClass Object
(
[0] => stdClass Object
(
[district] => stdClass Object
(
[state] => NY
[number] => 29
)
)
[1] => stdClass Object
(
[district] => stdClass Object
(
[state] => NY
[number] => 26
)
)
)
当我自己创建对象,然后执行 var_dump 时,我得到以下结果:
object(stdClass)#8 (2) {
[0]=>
object(stdClass)#4 (1) {
["district"]=>
object(stdClass)#5 (2) {
["state"]=>
string(2) "NY"
["number"]=>
string(2) "29"
}
}
[1]=>
object(stdClass)#6 (1) {
["district"]=>
object(stdClass)#7 (2) {
["state"]=>
string(2) "NY"
["number"]=>
string(2) "26"
}
}
}
I'm struggling to parse the below data using PHP. An API returns it, and I've tried various syntaxes. How do I return the data in a non-object way? Or, what's the syntax to call the data using the stdClass?
Could I convert this to one data based array, or even two? I'm lost when it comes to object-based data sets.
stdClass Object
(
[0] => stdClass Object
(
[district] => stdClass Object
(
[state] => NY
[number] => 29
)
)
[1] => stdClass Object
(
[district] => stdClass Object
(
[state] => NY
[number] => 26
)
)
)
When i create the object on my own, and then do a var_dump, I get this:
object(stdClass)#8 (2) {
[0]=>
object(stdClass)#4 (1) {
["district"]=>
object(stdClass)#5 (2) {
["state"]=>
string(2) "NY"
["number"]=>
string(2) "29"
}
}
[1]=>
object(stdClass)#6 (1) {
["district"]=>
object(stdClass)#7 (2) {
["state"]=>
string(2) "NY"
["number"]=>
string(2) "26"
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
他们可能在代码中将数组转换为对象(
$object = (object) $array
)。这样做的优点是从现在开始它将通过引用传递(这是对象的默认设置),缺点是对象完全无用(成员不能以数字开头 - 请参阅 PHP 文档中的正则表达式),直到您将其转换回来(PHP 确实允许一些非常神秘的事情):They are probably casting arrays to objects in their code (
$object = (object) $array
). This has the advantage that it will be passed by reference from now on (as is the default with objects) and the disadvantage that the object is completely useless (members cannot start with numbers - see the regex in PHP's docs) until you cast it back (PHP does allow some very mysterious things):使用:
基本上,您可以快捷地将字符串分配给变量,然后使用该变量作为对象访问器。
Use:
Basically You're short-cutting assigning a string to a variable, then using that variable as your object accessor.
我现在正在查看他们的代码,不幸的是,他们没有在类中公开选项,以便您以关联数组树与 stdClass 对象树的形式请求数据。
“问题”位于 class.sunlightlabs.php 中的第 96 行,
您有几个选择。
#1 in action
#2 in action
您还可以通过一些创造性的模式应用直接使用他们的类,但是他们已经大量使用了子类,这使事情变得相当复杂,所以我坚持使用这两种解决方案之一。
I'm looking at their code now, and unfortunately, they've not exposed the option in their class for you to request the data as a associative array tree vs a stdClass object tree.
The "problem" is at line 96 in class.sunlightlabs.php
You have a couple options.
#1 in action
#2 in action
You could also work with their class directly via some creative application of patterns, but they make heavy use of sublcasses already which complicates it quite a bit, so I'd stick to one of these two solutions.
您可以像这样迭代对象:
You can iterate through the object like this:
用户“null”在评论中建议了这一点,但想将其放在这里,这样就不会那么容易错过。
最好的选择是将 TRUE 作为 json_decode 中的第二个参数传递,即 json_decode($data, TRUE) ,这使其返回关联数组而不是类。因此,如果您有权访问源代码,请进行更改。
The user 'null' suggested this in the comments, but want to put it here so it's not missed so easly.
The best option is to pass TRUE as the second param in json_decode i.e. json_decode($data, TRUE) which makes it return associative arrays instead of classes. So if you have access to the source code – make that change.
Soulmerge 的这个解决方案对我来说效果很好:
非常感谢!
This solution from soulmerge just worked fine for me:
Thanks a lot!!