2013-08-29 7 views
1

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> 

그리고

enter image description here 배열의 인 print_r입니다 ... 등등 thumb_exists 등 레브, 같은 해제 필요한 컬럼의 일부를 잘라 싶습니다 :

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 
) 

당신은 내가 테이블에서 특정 coloumns를 제거하거나 배열에서 그 부분을 제거하는 방법에 대한 갈 것이라고 어떻게 내가 말할 수 바랍니다. 마커스

+0

지금까지 시도해 보셨습니까? 배열의 어느 부분을 제거 할 것인지 예를 들어주십시오. (좋아, 방금 화면 사이에 작은 선을 보았습니다!) – Sugar

+0

제목이 원하지 않는 열 –

답변

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> 

아마도 가장 쉬운 방법은 아니 겠지만, 아마도 가장 쉬운 방법 일 것입니다.

적절하게하려면 재귀 함수를 사용하면 지정한 필드의 설정을 해제하는 것이 도움이 될 수 있습니다. 이 경우, 당신이 할 수 있습니다 : 당신이 $headings = array_keys($fileMetadata['contents'][0]); 검색하기 전에

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); 

위가 수행해야합니다, 그들은 제거 전에 제목을하지 않는 방법을.

+0

+1에있는 경우 계속 진행하려면 foreach 루프에 조건을 추가하십시오. –

0

는 드롭 박스에없는 특정 있다는

감사합니다, 당신은 배열에서 요소를 제거하기 위해 unset (http://php.net/manual/en/function.unset.php)를 사용할 수 있습니다. 예를 들어
은 열 rev를 제거하고 thumb_exists 당신은 $fileMetadata['contents'] 값에 대한주기 (코드는 테스트되지 않음) 할 수

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