1

API の使用を練習するために Dropbox ファイル ブラウザを作成していますが、問題が発生しました。これが私のコードです:

<?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>

そして、rev、thumb_exists などの不要な列をいくつか切り取りたいと思います... ここに画像の説明を入力

これは、配列の 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
)

テーブルから特定の列を削除したり、配列からそれらの部分を削除したりする方法を教えてください。

ありがとう、マーカス

4

2 に答える 2

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>

最も美しい方法ではないかもしれませんが、おそらく最も簡単な方法の 1 つです。

適切に実行したい場合は、指定したフィールドの設定を解除する再帰関数が役立つ場合があります。この場合、これを行うことができます:

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]);、削除される前に見出しが取得されません。

于 2013-08-29T22:28:29.373 に答える