遍历多维数组 [英] Loop through a multidimensional array

查看:81
本文介绍了遍历多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用php和dropbox api开发应用程序,并试图遍历多维数组并输出到表.到目前为止,这是我的代码:

I am developing an application using php and the dropbox api and am trying to loop through a multidimensional array and output to a table. This is my code so far:

<?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的输出:

and this is the output from the print_r of the array:

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
)

我尝试了多种不同的方法,例如:

I have tried a combination of different methods from posts such as:

  • Trouble Looping through multidimensional array
  • PHP foreach loop through multidimensional array
  • PHP foreach loop through multidimensional array
  • PHP: Loop Through Multidimensional Array

他们都没有工作.

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

I was hoping someone would be able to give me a bit of code that will loop through the array and output it into a table. It is also only the contents array that needs to be put into the table

预先感谢,马库斯

推荐答案

内联和阻止php的一点点就可以了.其余的格式取决于您; D

A little bit of inline and block php will do the basics. The rest of the formatting is up to you ;D

所有foreach都已将这些值用作参考,以加快速度.

All foreach have been done with the values as references in an effort to speed things up.

修订后的答案

基于新信息,由于您希望其布局与顶部的标题相同,因此您可以尝试执行以下操作:

Based on new information, since you want it laid out with the headings at the top you'd want to try something like this:

<?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用作标题,打印出标题行,然后通过引用直接从数组中浏览数据行.

First line grabs the array_keys as headings, prints out the heading rows and then goes through the data rows directly from the array by reference.

以前的修订

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

This was my last answer, it displays the table with the headings on the left, with the data displayed in columns.

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

由于表的布局方式,您可以使用一些改组就可以正确地迭代数组.不过,可能有上千种不同的编写方式.

Because of the way tables are laid out, you can just use a little bit of shuffling to get an array to iterate correctly. There's probably a good thousand different ways to write this though.

这篇关于遍历多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆