2

I am trying to figure out how i can loop through this stdClass Object. I am trying to get the path value of each contents array item [0]-[?].

The Object array looks like this:

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 
3
  • Just remove the inner foreach loop, var_dump($arr); and you'll see. Btw, $arr is a misleading variable name. It should be $property as you are iterating over an object's properties Commented Mar 30, 2014 at 0:59
  • @hek2mgl then i get an error of Notice: Trying to get property of non-object in C:\wamp\www\test\sample.php on line 78 Commented Mar 30, 2014 at 1:02
  • Think about it again, try it again and you'll see. Once you are enlightened you'll see that there is not much to say Commented Mar 30, 2014 at 1:04

2 Answers 2

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')
}}}
1
  • OK, the echo wasn't treating $value as an object. Try now? Commented Mar 30, 2014 at 3:06
0

The problem is that foreach wants to receive an array or an object, as stated by

Warning: Invalid argument supplied for foreach() in C:\wamp\www\test\sample.php on line 78

when doing the foreach on line 77 you give it a whole dropbox object. 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. just give the right content to the first foreach:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.