[英]PHP Array stdClass Object
I have this array 我有这个数组
Array (
[0] => stdClass Object (
[id] => 252062474)
[1] => stdClass Object (
[id] => 252062474)
[3] => stdClass Object (
[id] => 252062474)
)
I need echo all of id's 我需要回显所有ID
I tried, 我试过了,
foreach($result as $item) {
echo $item->id;
}
but no luck 但没有运气
I try json_decode()
我尝试
json_decode()
again no luck I use php 5.5.8 再次没有运气,我使用PHP 5.5.8
I know this work 我知道这工作
echo $item[0]->id;
but i don't how many index is there 但我没有多少索引
any idea? 任何想法?
Maybe you are confused on foreach()
. 也许您对
foreach()
感到困惑。 If this works: 如果可行:
echo $item[0]->id;
Then you would need: 然后,您将需要:
foreach($item as $result) {
echo $result->id;
}
Try this- 尝试这个-
Code 码
foreach($result as $p) {
echo $p['id'] . "<br/>";
}
Output 输出量
252062474
252062474
252062474
This array could be looped through and data could be retrieved. 该数组可以循环通过,并且可以检索数据。 As you are saying its not working I have added the following code to illustrate how it works right from generating the array for you.
正如您所说的那样,我添加了以下代码来说明如何从为您生成数组后正确地工作。
// generating an array like you gave in the example. Note that ur array has same value
// for all the ids in but my example its having different values
$arr = array();
$init = new stdClass;
$init->id = 252062474 ;
$arr[] = $init;
$init = new stdClass;
$init->id = 252062475 ;
$arr[] = $init;
$init = new stdClass;
$init->id = 252062476 ;
$arr[] = $init;
print_r($arr);
The above array is same as yours 上面的数组和你的一样
Array ( [0] => stdClass Object ( [id] => 252062474 )
[1] => stdClass Object ( [id] => 252062475 )
[2] => stdClass Object ( [id] => 252062476 )
)
Now the following code will loop through and get the data as 现在,以下代码将循环遍历并以
foreach($arr as $key=>$val){
echo $key.' ID is :: '.$val->id;
echo '<br />';
}
The output will be 输出将是
0 ID is :: 252062474
1 ID is :: 252062475
2 ID is :: 252062476
Try this 尝试这个
foreach($result as $item) {
$item = (array)$item;
echo $item['id'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.