user3303790
user3303790

Reputation: 137

Reading dir from array

i'm using a dropbox api, and this is a part of my script:

$folders = $dropbox->GetMetadata("");

echo '<pre>';
print_r($folders).'
echo </pre>';

It returns an array like this:

stdClass Object
(
    [hash] => 9539e595b82fce68ee1bcda37a23848f
    [thumb_exists] => 
    [bytes] => 0
    [path] => /
    [is_dir] => 1
    [icon] => folder
    [root] => dropbox
    [contents] => Array
    (
        [0] => stdClass Object
            (
                [bytes] => 0
                [rev] => e8b1169b0b4
                [revision] => 3723
                [icon] => folder_camera
                [path] => /Camera Uploads
                [is_dir] => 1
                [thumb_exists] => 
                [root] => dropbox
                [modified] => Sun, 04 Jan 2015 08:12:03 +0000
                [size] => 0 bytes
            )

        [1] => stdClass Object
            (
                [rev] => e5a1169b0b4
                [thumb_exists] => 1
                [path] => /DSC_0346.JPG
                [is_dir] => 
                [client_mtime] => Sun, 14 Dec 2014 08:52:25 +0000
                [icon] => page_white_picture
                [bytes] => 1777550
                [modified] => Sun, 14 Dec 2014 08:52:25 +0000
                [size] => 1.7 MB
                [root] => dropbox
                [mime_type] => image/jpeg
                [revision] => 3674
            )

But i just cant figure out how i list only all the directory names in one list. The directory names are named [path] in the array.

Can someone explain it to me?

Upvotes: 1

Views: 40

Answers (3)

Riad
Riad

Reputation: 3850

Try this:

$paths[]  = $folders->path ; //return the root path(/)

foreach($folders->contents as $pathObj)
{
   if ($pathObj->is_dir==1) {   

       $paths[]  = $pathObj->path ;
   }
}

print_r($paths);

Upvotes: 0

Alex
Alex

Reputation: 17289

The question is not 100% clear.

So if you want to list folders only do this:

$folders = $dropbox->GetMetadata("");

echo '<pre>';
foreach($folders->contents as $item) {
  if ($item->is_dir==1) {
    echo $item->path.'<br />';
  }
}

echo '</pre>';

hope my guess is what you are asking for

Upvotes: 3

jeroen
jeroen

Reputation: 91734

You need to loop over the contents property of your object as that is the array containing the folder objects where the path and is_dir properties are stored:

foreach ($folders->contents as $obj)
{
  if ($obj->is_dir === 1)
  {
    echo $obj->path;
  }
}

Upvotes: 1

Related Questions