Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 
share|improve this question
    
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 –  hek2mgl Mar 30 '14 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 –  StealthRT Mar 30 '14 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 –  hek2mgl Mar 30 '14 at 1:04

2 Answers 2

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')
}}}
share|improve this answer
    
Sorry but that didnt work. –  StealthRT Mar 30 '14 at 2:10
    
OK, the echo wasn't treating $value as an object. Try now? –  JBES Mar 30 '14 at 3:06

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;
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.