PHP / JSON - stdClass的对象 [英] PHP/JSON - stdClass Object
问题描述
我是pretty新阵列还。我需要一些帮助 - 我有一些JSON和我通过一些PHP,基本上解析JSON和德codeS运行它,如下所示:
stdClass的对象
(
[2010091907] => stdClass的对象
(
[首页] => stdClass的对象
(
[评分] => stdClass的对象
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
) [简称] => ATL
[转] => 2
)
这实际上不胜枚举和 - 但是 - 我的问题是 stdClass的对象
部分。我需要能够在调用此循环,然后通过每个部分(家庭,得分,简称,对等)迭代。我会如何呢?
您可以使用 get_object_vars()
来获取对象的属性的数组,或致电 json_de code()
与 json_de code($字符串,真实);
来得到一个关联数组<。 / p>
示例:
&LT; PHP
$富=阵列('123456'=&GT;
阵列('酒吧'=&GT;
阵列('富'=大于1,'巴'=→2)));
//为研究对象
后续代码var_dump($ OPT1 = json_de code(json_en code($富)));回声$ opt1-&GT; {'123456'} - &GT;酒吧,&GT; foo的;的foreach(get_object_vars($ opt1-&GT; {'123456'} - &GT;巴)为$关键=&GT; $值){
回声$键':'$ value.PHP_EOL;
}//为数组
后续代码var_dump($ OPT2 = json_de code(json_en code($ foo的),真));回声$ OPT2 ['123456'] ['酒吧'] ['富'];的foreach($ OPT2 ['123456'] ['酒吧']为$关键=&GT; $值){
回声$键':'$ value.PHP_EOL;
}
?&GT;
输出:
对象(stdClass的)#1(1){
[123456] =&GT;
对象(stdClass的)#2(1){
[条] =&GT;
对象(stdClass的)#3(2){
[富] =&GT;
INT(1)
[条] =&GT;
INT(2)
}
}
}
1
富:1
酒吧:2阵列(1){
[123456] =&GT;
阵列(1){
[条] =&GT;
阵列(2){
[富] =&GT;
INT(1)
[条] =&GT;
INT(2)
}
}
}
1
富:1
酒吧:2
I'm pretty new to arrays still. I need some help - I have some JSON, and I've run it through some PHP that basically parses the JSON and decodes it as follows:
stdClass Object
(
[2010091907] => stdClass Object
(
[home] => stdClass Object
(
[score] => stdClass Object
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
)
[abbr] => ATL
[to] => 2
)
This actually goes on and on - BUT - my problem is the stdClass Object
part. I need to be able to call this in a for loop and then iterate through each section (home, score, abbr, to, etc). How would I go about this?
You can use get_object_vars()
to get an array of the object's properties, or call json_decode()
with json_decode($string,true);
to get an associative array.
Example:
<?php
$foo = array('123456' =>
array('bar' =>
array('foo'=>1,'bar'=>2)));
//as object
var_dump($opt1 = json_decode(json_encode($foo)));
echo $opt1->{'123456'}->bar->foo;
foreach(get_object_vars($opt1->{'123456'}->bar) as $key => $value){
echo $key.':'.$value.PHP_EOL;
}
//as array
var_dump($opt2 = json_decode(json_encode($foo),true));
echo $opt2['123456']['bar']['foo'];
foreach($opt2['123456']['bar'] as $key => $value){
echo $key.':'.$value.PHP_EOL;
}
?>
Output:
object(stdClass)#1 (1) {
["123456"]=>
object(stdClass)#2 (1) {
["bar"]=>
object(stdClass)#3 (2) {
["foo"]=>
int(1)
["bar"]=>
int(2)
}
}
}
1
foo:1
bar:2
array(1) {
[123456]=>
array(1) {
["bar"]=>
array(2) {
["foo"]=>
int(1)
["bar"]=>
int(2)
}
}
}
1
foo:1
bar:2
这篇关于PHP / JSON - stdClass的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!