简体   繁体   English

PHP stdClass对象数组循环

[英]PHP stdClass Object array looping

I am trying to figure out how i can loop through this stdClass Object. 我试图弄清楚如何循环遍历此stdClass对象。 I am trying to get the path value of each contents array item [0]-[?] . 我试图获取每个内容数组项[0]-[?]路径值。

The Object array looks like this: 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...

Currently i am trying the following code to loop: 目前,我正在尝试以下代码循环:

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: }

But i keep getting this error: 但我不断收到此错误:

( ! ) 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 

Try the following: 请尝试以下操作:

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')
}}}

The problem is that foreach wants to receive an array or an object, as stated by 问题是, foreach想要接收数组或对象,如

Warning: Invalid argument supplied for foreach() in C:\\wamp\\www\\test\\sample.php on line 78 警告:第78行的C:\\ wamp \\ www \\ test \\ sample.php中为foreach()提供了无效的参数

when doing the foreach on line 77 you give it a whole dropbox object. 当在第77行进行foreach时,您为其提供了一个完整的dropbox对象。 then, on line 78, you do a foreach on that general object properties, but the first key of the general object ( hash ) is not an array , giving you your error. 然后,在第78行上,您对该常规对象属性进行了foreach操作, 但是常规对象的第一个键( hash )不是array ,这给您带来了错误。 just give the right content to the first foreach: 只需为第一个foreach提供正确的内容:

$object = $dropbox->GetMetadata($file->path);
foreach ($object->contents as $arr) {
  foreach ($arr as $obj) {
    $path = $obj->path;
    echo $path;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM