2013-08-29 92 views
1

我正在制作一个Dropbox文件浏览器来练习使用API​​并提出了一个问题。这里是我的代码:删除数组的某些部分

<?php 
    $fileMetadata = $dbxClient->getMetadataWithChildren("/upload"); 
    $headings = array_keys($fileMetadata['contents'][0]); 
?> 
<br /><br /><br /> 
    <table border="2" class="table table-striped table-bordered table-hover"> 
     <tr> 
      <?php foreach($headings as &$heading): ?> 
        <th><?php echo $heading; ?></th> 
       <?php endforeach; ?> 
      </tr> 
      <?php foreach($fileMetadata['contents'] as &$file): ?> 
       <tr> 
        <?php foreach($file as &$data): ?> 
         <td><?php echo $data; ?></td> 
        <?php endforeach; ?> 
       </tr> 
      <?php endforeach; ?> 
     </table> 

而且我想取消一些非必要的列,如转速,thumb_exists等,等的... enter image description here

这是数组的print_r:

Array 
(
    [hash] => d023a1738d460f667d383cb4f57bc769 
    [revision] => 65 
    [rev] => 411389e826 
    [thumb_exists] => 
    [bytes] => 0 
    [modified] => Wed, 28 Aug 2013 20:28:34 +0000 
    [path] => /upload 
    [is_dir] => 1 
    [icon] => folder 
    [root] => app_folder 
    [contents] => Array 
     (
      [0] => Array 
       (
        [revision] => 81 
        [rev] => 511389e826 
        [thumb_exists] => 1 
        [bytes] => 1996564 
        [modified] => Wed, 28 Aug 2013 21:32:10 +0000 
        [client_mtime] => Wed, 28 Aug 2013 21:32:11 +0000 
        [path] => /upload/08-nigellas-chocolate-chip-muffins.jpg 
        [is_dir] => 
        [icon] => page_white_picture 
        [root] => dropbox 
        [mime_type] => image/jpeg 
        [size] => 1.9 MB 
       ) 

      [1] => Array 
       ( 
        [revision] => 79 
        [rev] => 4f1389e826 
        [thumb_exists] => 1 
        [bytes] => 22848 
        [modified] => Wed, 28 Aug 2013 21:14:39 +0000 
        [client_mtime] => Wed, 28 Aug 2013 21:14:39 +0000 
        [path] => /upload/1376243030_guestion.png 
        [is_dir] => 
        [icon] => page_white_picture 
        [root] => dropbox 
        [mime_type] => image/png 
        [size] => 22.3 KB 
       ) 

      [2] => Array 
       (
        [revision] => 80 
        [rev] => 501389e826 
        [thumb_exists] => 
        [bytes] => 54772 
        [modified] => Wed, 28 Aug 2013 21:26:19 +0000 
        [client_mtime] => Wed, 28 Aug 2013 21:26:19 +0000 
        [path] => /upload/BT_screen_quiz.java 
        [is_dir] => 
        [icon] => page_white_cup 
        [root] => dropbox 
        [mime_type] => text/x-java 
        [size] => 53.5 KB 
       ) 

      [3] => Array 
       (
        [revision] => 77 
        [rev] => 4d1389e826 
        [thumb_exists] => 
        [bytes] => 1679 
        [modified] => Wed, 28 Aug 2013 20:59:53 +0000 
        [client_mtime] => Wed, 28 Aug 2013 20:59:53 +0000 
        [path] => /upload/login.php 
        [is_dir] => 
        [icon] => page_white_php 
        [root] => dropbox 
        [mime_type] => text/php 
        [size] => 1.6 KB 
       ) 

      [4] => Array 
       ( 
        [revision] => 78 
        [rev] => 4e1389e826 
        [thumb_exists] => 
        [bytes] => 2037 
        [modified] => Wed, 28 Aug 2013 21:00:56 +0000 
        [client_mtime] => Wed, 28 Aug 2013 21:00:56 +0000 
        [path] => /upload/signup.php 
        [is_dir] => 
        [icon] => page_white_php 
        [root] => dropbox 
        [mime_type] => text/php 
        [size] => 2 KB 
       ) 

     ) 

    [size] => 0 bytes 
) 

请你能告诉我如何去除表中的某些颜色或从阵列中删除这些部分。

感谢, 马库斯

+0

到目前为止你有尝试过吗?举个例子,说明你想要删除的数组的哪一部分。 (好吧,刚刚看到你的截图之间有一点点线) – Sugar

+0

尝试在你的foreach循环中添加条件继续,如果你的标题是在不需要的列之一 –

回答

2

我们再次见面!既然我在这里潜伏,我不妨给一个答案。为了尽量减少你已经拥有的工作量,而不是通过挖掘数组来取消设置值,你可以创建一个标题/列的数组来删除并使用它来检查你的foreach值。

<?php 
    $fileMetadata = $dbxClient->getMetadataWithChildren("/upload"); 
    $headings = array_keys($fileMetadata['contents'][0]); 

    //Add field names to remove in array below 
    $remove = array('is_dir', 'client_mtime'); 
?> 
<br /><br /><br /> 
<table border="2" class="table table-striped table-bordered table-hover"> 
    <tr> 
     <?php foreach($headings as &$heading): ?> 

      <!-- If statement added below, excludes defined fields to remove --> 
      <?php if(!in_array($heading, $remove)): ?> 
       <th><?php echo $heading; ?></th> 
      <?php endif; ?> 

     <?php endforeach; ?> 
    </tr> 
    <?php foreach($fileMetadata['contents'] as &$file): ?> 
     <tr> 

      <!-- Changed foreach to pull $key as well --> 
      <?php foreach($file as $key => &$data): ?> 

       <!-- Added another if statement --> 
       <?php if(!in_array($key, $remove)): ?> 
        <td><?php echo $data; ?></td> 
       <?php endif; ?> 

      <?php endforeach; ?> 
     </tr> 
    <?php endforeach; ?> 
</table> 

可能不是最漂亮的方法,但它可能是最简单的方法之一。

如果您想要做到这一点,有一个递归函数来取消设置您指定的任何字段可能会有所帮助。在这种情况下,你可以这样做:

function removeFields($fields, &$array) 
{ 
    foreach($array as $key => &$value) { 

     if(is_array($value)) { 

      removeFields($fields, $value); 
     } 
     else { 

      if(in_array($key, $fields)) { 

       unset($array[$key]); 
      } 
     } 
    } 
} 
//Still have to define values you don't want 
$remove = array('thumb_exists', 'is_dir', 'root'); 

removeFields($remove, $fileMetadata); 

print_r($fileMetadata); 

以上应该可以检索$headings = array_keys($fileMetadata['contents'][0]);之前完成,这样,他们被删除之前,它不会标题。

+0

+1为你的快速答案,我正在写它。 –

0

暂时不具体到Dropbox的,你可以使用unsethttp://php.net/manual/en/function.unset.php)以去除数组中的元素。
例如,删除列revthumb_exists你可以$fileMetadata['contents']值周期(代码未测试):

while(list($key,$value)=each($fileMetadata['contents'])) { 
    unset($fileMetadata['contents'][$key]['rev']); 
    unset($fileMetadata['contents'][$key]['thumb_exists']); 
}