从数组中读取dir

时间:2015-01-08 18:30:55

标签: php arrays api

我正在使用dropbox api,这是我脚本的一部分:

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

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

它返回一个这样的数组:

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]。

有人可以向我解释一下吗?

3 个答案:

答案 0 :(得分:3)

问题不是100%明确。

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

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

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

echo '</pre>';

希望我的猜测是你要求的

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

试试这个:

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

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

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

print_r($paths);