循环遍历多维数组

时间:2013-08-28 22:45:48

标签: php arrays loops multidimensional-array foreach

我正在使用php和dropbox api开发一个应用程序,我正在尝试循环一个多维数组并输出到一个表。到目前为止,这是我的代码:

<?php
session_start();

# Include the Dropbox SDK libraries
require_once "Dropbox/autoload.php";
use \Dropbox as dbx;

// Create connection
$con = mysqli_connect(
    "localhost", "sintegra_aggre", "*******", "sintegra_aggregator"
);

// Check connection
if (mysqli_connect_errno($con)) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if (isset($_SESSION['uid'])) {
    $password = $_SESSION['pass'];
    $username = $_SESSION['user'];
    $email = mysqli_query($con, "
        SELECT * 
        FROM main_users
        WHERE
            password='$password'
            AND username='$username'
    ");

    if ($email >= 1) {
        // let them stay on page
    } else {
        header("Location: logout.php");
    }
} else {
    header("Location: login.php");
}

$accessToken = $_SESSION['accessToken'];

$dbxClient = new dbx\Client($accessToken, "PHP-Example/1.0");

$folderMetadata = $dbxClient->getMetadataWithChildren("/upload");

foreach ($folderMetadata as $value) {
    echo $value;
    foreach ($value as $val) {
        echo $val . "<br />";
    }
    echo "<br /><br />";
}
?>

这是数组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
)

我尝试过各种方法的组合,例如:

并且没有一个有效。

我希望有人能够给我一些代码,这些代码将遍历数组并将其输出到表中。它也只是需要放入表格的内容数组

提前致谢,Marcus

1 个答案:

答案 0 :(得分:1)

一点内联和阻止php将做基础知识。其余格式由您决定; D

为了加快速度,所有的foreach都已经完成了值作为参考。

修订答案

根据新信息,由于您希望它的标题位于顶部,您需要尝试这样的事情:

<?php $headings = array_keys($array['contents'][0]); ?>

<table>
    <tr>
        <?php foreach( $headings as &$heading ): ?>
            <th><?php echo $heading; ?></th>
        <?php endforeach; ?>
    </tr>
    <?php foreach( $array['contents'] as &$file ): ?>
        <tr>
            <?php foreach( $file as &$data ): ?>
                <td><?php echo $data; ?></td>
            <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
</table>

第一行将array_keys作为标题抓取,打印出标题行,然后直接从数组中通过引用遍历数据行。

上一次修订

这是我的最后一个答案,它显示左侧标题的表格,数据显示在列中。

<?php
    $output = array();

    foreach( $test['contents'] as &$file )
    {
        foreach( $file as $heading => &$value )
        {
            $output[$heading][] = $value;
        }
    }
?>

<table>
    <?php foreach( $output as $heading => &$data): ?>
        <tr>
            <th><?php echo $heading; ?></th>
           <?php foreach( $data as &$value ): ?>
               <td><?php echo $value; ?></td>
           <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
</table>

由于表的布局方式,您可以使用一点点混洗来使数组正确迭代。尽管如此,可能有很多不同的方法可以写出来。