我试图弄清楚如何循环这个stdClass对象。我正在尝试获取每个内容数组项[0] - [?] 的路径值。
Object数组如下所示:
stdClass Object
(
[hash] => zzzzzzzzzzzzzzzzzzzzzzzzzzzz
[revision] => 22
[rev] => 161f5f4043
[thumb_exists] =>
[bytes] => 0
[modified] => Sun, 23 Mar 2014 18:05:38 +0000
[path] => /Camera Uploads
[is_dir] => 1
[icon] => folder_photos
[root] => dropbox
[contents] => Array
(
[0] => stdClass Object
(
[revision] => 47
[rev] => 2f1f5f4043
[thumb_exists] => 1
[bytes] => 3212196
[modified] => Sun, 23 Mar 2014 18:07:05 +0000
[client_mtime] => Wed, 05 Feb 2014 19:10:14 +0000
[path] => /Camera Uploads/2014-02-05 14.10.13.jpg
[is_dir] =>
[icon] => page_white_picture
[root] => dropbox
[mime_type] => image/jpeg
[size] => 3.1 MB
)
[1] => stdClass Object
(
etc etc...
目前我正在尝试以下代码循环:
line 75: print_r ($dropbox->GetMetadata($file->path));
line 77: foreach ($dropbox->GetMetadata($file->path) as $arr) {
line 78: foreach ($arr as $obj) {
line 79: $path = $obj->path;
line 80: echo $path;
line 81: }
line 82: }
但我一直收到这个错误:
( ! ) Warning: Invalid argument supplied for foreach() in
C:\wamp\www\test\sample.php on line 78
Call Stack
# Time Memory Function Location
1 0.0011 157592 {main}( ) ..\sample.php:0
答案 0 :(得分:0)
尝试以下方法:
foreach($dropbox->GetMetadata($file->path) as $result){ //Process each value as $result
if(is_array($result)){ //If $result is an array continue ('contents')
foreach($result as $value){ //Process each value from $result (0..x)
echo"{$value->path}\n"; //Echo what you are looking for ('contents'>'0'>'path')
}}}
答案 1 :(得分:0)
问题是foreach想要接收数组或对象,如
所述警告:为foreach()提供的参数无效 第78行的C:\ wamp \ www \ test \ sample.php
在77行上做foreach时,你给它一个完整的Dropbox对象。然后,在第78行,你对常规对象属性做了一个foreach,但是一般对象的第一个键(hash
)不是一个数组,给你错误。只是给第一个foreach提供正确的内容:
$object = $dropbox->GetMetadata($file->path);
foreach ($object->contents as $arr) {
foreach ($arr as $obj) {
$path = $obj->path;
echo $path;
}
}