简体   繁体   English

PHP-遍历数组并在HTML表中转置数组数据

[英]PHP - Iterating Over Arrays and Transposing Array Data in HTML Table

I am a bit stuck and need your help! 我有点卡住,需要您的帮助! I have an array of students: 我有很多学生:

Students: 学生们:

  Array ( [John] [Jerry] [Sam] [Amanda] ) 

I iterate over each of the students (no problems here...) and for each student, I have their meal schedule for two weeks (in the array below, I have a schedule for John (only) from 31st March till 13th of April --- Jerry, Sam, and Amanda's are in the same structure, but with different values etc.) 我遍历每个学生(这里没有问题...),对于每个学生,我有他们两周的用餐时间表(在下面的数组中,我有3月31日至4月13日的John时间表) --- Jerry,Sam和Amanda的结构相同,但值不同等。)

Meal Schedule: 用餐时间表:

 Array ( [31-MAR-17] => Array ( [Apples] => 1 [Oranges] => [Peaches] => [Berries] => ) 
[01-APR-17] => Array
    (
        [Apples] => 
        [Oranges] => 
        [Peaches] => 2
        [Berries] => 
    )

[02-APR-17] => Array
    (
        [Apples] => 
        [Oranges] => 2
        [Peaches] => 
        [Berries] => 
    )

[03-APR-17] => Array
    (
        [Apples] => 2
        [Oranges] => 
        [Peaches] => 2
        [Berries] => 
    )

[04-APR-17] => Array
    (
        [Apples] => 
        [Oranges] => 
        [Peaches] => 
        [Berries] => 50
    )

[05-APR-17] => Array
    (
        [Apples] => 1
        [Oranges] => 1
        [Peaches] => 1
        [Berries] => 
    )

[06-APR-17] => Array
    (
        [Apples] => 
        [Oranges] => 2
        [Peaches] => 
        [Berries] => 
    )

[07-APR-17] => Array
    (
        [Apples] => 
        [Oranges] => 
        [Peaches] => 
        [Berries] => 50
    )

[08-APR-17] => Array
    (
        [Apples] => 
        [Oranges] => 
        [Peaches] => 3
        [Berries] => 
    )

[09-APR-17] => Array
    (
        [Apples] => 3
        [Oranges] => 
        [Peaches] => 
        [Berries] => 20
    )

[10-APR-17] => Array
    (
        [Apples] => 
        [Oranges] => 2
        [Peaches] => 
        [Berries] => 15
    )

[11-APR-17] => Array
    (
        [Apples] => 
        [Oranges] => 
        [Peaches] => 1
        [Berries] => 20
    )

[12-APR-17] => Array
    (
        [Apples] => 2
        [Oranges] => 3
        [Peaches] => 
        [Berries] => 
    )

[13-APR-17] => Array
    (
        [Apples] => 1
        [Oranges] => 
        [Peaches] => 2
        [Berries] => 
    )

)

Here's where I am stuck-- 这就是我被困住的地方

I need the layout to be as follows: 我需要的布局如下:

期望的输出

and the same desired output would be displayed for the other three students... based on their own meal schedules 根据其他人的用餐时间表,其他三个学生将获得相同的期望输出

I have researched and have yet to come across a similar scenario on this site or on the net. 我已经研究过,但尚未在此站点或网络上遇到类似的情况。

How can I go about achieving this? 我该如何实现呢? Any and all help is greatly appreciated! 任何帮助都将不胜感激!

Thanks! 谢谢!

<table>
   <tr>
       <?php foreach ($johnArray as $date=>$food) { ?>
       <td><?= date('m/d', strtotime($date)) ?></td>
       <?php } ?>
       <td>Type</td>
   </tr>
   <!-- Apple row -->
   <tr>
       <?php foreach ($johnArray as $date=>$food) { ?>
       <td><?= $food['Apples'] ?></td>
       <?php } ?>
       <td>Apple</td>
   </tr>
</table>

This is just a snippet to help you get started. 这只是一个片段,可以帮助您入门。 You should be able to figure the other rows out yourself. 您应该能够自己找出其他行。

This example is for one person (im assuming each person has their own table). 本示例适用于一个人(假设每个人都有自己的桌子)。 You first generate the first row with the dates, followed by the Type column. 您首先要生成带有日期的第一行,然后是“类型”列。 So you loop through the dates, which is the key in the array and create a column for each date. 因此,您可以遍历日期(这是数组中的键)并为每个日期创建一列。 After which you add the column for Type. 之后,添加“类型”列。

You do the same approach for the food values as well. 您对食物的价值也采取相同的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM