[英]stdClass Object and array how to using php
I'm trying to get the twelve ids that this structure shows: 我正在尝试获得此结构显示的十二个ID:
stdClass Object
(
[checkins] => stdClass Object
(
[count] => 12
[items] => Array
(
[0] => stdClass Object
(
[venue] => stdClass Object
(
[id] => 4564654646456
.
.
I do: 我做:
$checkins = $fsObjUnAuth->get("/users/self/checkins");
$count = $checkins ->response->checkins->count; // so I can get 12
for( $i = 0; $i < $count; $i ++)
{
$a1[] = $checkins['items'][$i]['venue']['id']; //two tries
$a2[] = $checkins ->response->checkins->items->$i->venue->id;
echo $i; echo ": ";
echo $a1;echo"<br>";
echo $a2;echo"<br>"
}
But I get: Fatal error: Cannot use object of type stdClass as array in line.. 但我得到:致命错误:不能使用stdClass类型的对象作为数组行。
Please can someone show me how to do this? 请有人告诉我该怎么做?
Thanks a lot 非常感谢
You cannot access object members via the array subscript operator []
. 您无法通过数组下标operator
[]
访问对象成员。
You have to use the ->
operator: 你必须使用
->
运算符:
$x = new StdClass();
$x->member = 123;
In your case you'll have to use a mixture, since you have an object ( $checkins
) with a member ( $items
) which is an array, which contains additional objects. 在你的情况下,你将不得不使用混合物,因为你有一个对象(
$checkins
)与一个成员( $items
),这是一个数组,其中包含其他对象。
$a1[] = $checkins->items[$i]->venue->id;
Here is a simple solution to convert a stdClass Object in array in php with function get_object_vars
这是一个简单的解决方案,使用函数
get_object_vars
在php中的数组中转换 stdClass对象
Look at : http://php.net/manual/fr/function.get-object-vars.php 请看: http : //php.net/manual/fr/function.get-object-vars.php
Ex : 例如:
debug($array);
$var = get_object_vars($array);
debug($var);
Or replace debug
by print_r
或者用
print_r
替换debug
I'm use CakePHP framework 我使用的是CakePHP框架
Cdt CDT
change 更改
$a1[] = $checkins['items'][$i]['venue']['id'];
changed to 变成
$a1[] = $checkins->items[$i]->venue->id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.