从数组中读取目录

Reading dir from array

我正在使用 Dropbox API,这是我脚本的一部分:

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

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

它returns一个像这样的数组:

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
            )

但我就是想不通如何在一个列表中只列出所有目录名称。 目录名称在数组中命名为 [path]。

谁能给我解释一下?

您需要遍历对象的 contents 属性,因为这是包含存储 pathis_dir 属性的文件夹对象的数组:

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

问题不是 100% 清楚。

因此,如果您只想列出文件夹,请执行以下操作:

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

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

echo '</pre>';

希望我的猜测是你所要求的

试试这个:

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

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

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

print_r($paths);