PHP - Foreach Display Array以不同的方式

时间:2014-11-10 19:19:31

标签: php html arrays foreach

我有这个数组:

Array
(
    [Saturday] => Array
        (
            [11:00am] => 0
            [12:00pm] => 0
            [1:00pm] => 0
            [2:00pm] => 0
            [3:00pm] => 0
            [4:00pm] => 0
            [5:00pm] => 0
        )

    [Sunday] => Array
        (
            [11:00am] => 0
            [12:00pm] => 0
            [1:00pm] => 0
            [2:00pm] => 0
            [3:00pm] => 0
            [4:00pm] => 0
            [5:00pm] => 0
        )

    [Monday] => Array
        (
            [11:00am] => 0
            [12:00pm] => 0
            [1:00pm] => 0
            [2:00pm] => 0
            [3:00pm] => 0
            [4:00pm] => 0
            [5:00pm] => 0
        )

    [Tuesday] => Array
        (
            [11:00am] => 0
            [12:00pm] => 0
            [1:00pm] => 0
            [2:00pm] => 0
            [3:00pm] => 0
            [4:00pm] => 0
            [5:00pm] => 0
        )

    [Wednesday] => Array
        (
            [11:00am] => 0
            [12:00pm] => 0
            [1:00pm] => 0
            [2:00pm] => 0
            [3:00pm] => 0
            [4:00pm] => 0
            [5:00pm] => 0
        )

    [Thursday] => Array
        (
            [11:00am] => 0
            [12:00pm] => 0
            [1:00pm] => 0
            [2:00pm] => 0
            [3:00pm] => 0
            [4:00pm] => 0
            [5:00pm] => 0
        )

    [Friday] => Array
        (
            [11:00am] => 0
            [12:00pm] => 0
            [1:00pm] => 0
            [2:00pm] => 0
            [3:00pm] => 0
            [4:00pm] => 0
            [5:00pm] => 0
        )

)

我想要做的是将这个数组显示在一个表中,如下所示:

foreach($array as $row => $value){
    echo '<tr>';
    echo '<th>' . $row . '</th>';
    foreach($value as $x => $y){
        echo '<td>' . $x . '</td>';
    }
    echo '</tr>';

}

给了我这个:

Saturday    11:00am 12:00pm 1:00pm  2:00pm  3:00pm  4:00pm  5:00pm
Sunday  11:00am 12:00pm 1:00pm  2:00pm  3:00pm  4:00pm  5:00pm
Monday  11:00am 12:00pm 1:00pm  2:00pm  3:00pm  4:00pm  5:00pm
Tuesday 11:00am 12:00pm 1:00pm  2:00pm  3:00pm  4:00pm  5:00pm
Wednesday   11:00am 12:00pm 1:00pm  2:00pm  3:00pm  4:00pm  5:00pm
Thursday    11:00am 12:00pm 1:00pm  2:00pm  3:00pm  4:00pm  5:00pm
Friday  11:00am 12:00pm 1:00pm  2:00pm  3:00pm  4:00pm  5:00pm

但我希望像这样显示:

Saturday Sunday
11:00am 11:00am 
12:00pm 12:00pm 
1:00pm  1:00pm  
2:00pm  2:00pm  
3:00pm  3:00pm  
4:00pm  4:00pm  
对于每个日期

等等,我是否必须更改我的阵列或我的foreach?我该如何解决这个问题?我希望这是有道理的。

4 个答案:

答案 0 :(得分:1)

这是一种提供有效HTML5数据表的功能方法。

// Shift keys to convert into an array of 1-dimensional arrays
$ordered = array_map(function($day){
    return array_keys($day);
}, $data);

$headers = array_map(function($a){
    return "<td>$a</td>";
}, array_keys($ordered));

// Find day with highest number of values
$max_indice = max( array_map(function($day){
    return count($day) - 1;
}, $ordered) );

// Split data into rows.
$rows = array_map(function($i) use ($ordered) {
    return '<tr>' . join( array_map(function($day) use ($i){
        return '<td>' . $day[$i] . '</td>';
    }, $ordered)) . '</tr>';
}, range(0, $max_indice));

echo "
<table>
    <caption>My Table</caption>
    <thead>".join($headers,'\n')."</thead>
    <tbody>".join($rows, '\n')."</tbody>
</table>";

鉴于$data

$data = array(
    'Saturday' => array (
            '11:00am' => 0,
            '12:00pm' => 0,
            '1:00pm' => 0,
            '2:00pm' => 0
    ),
    'Sunday' => array(
            '11:00am' => 0,
            '12:00pm' => 0,
            '1:00pm' => 0
    )
);

输出(为了便于阅读而缩进):

screenshot with minimal css applied

<table>
    <caption>My Table</caption>
    <thead>
       <td>Saturday</td>
       <td>Sunday</td>
    </thead>
    <tbody>
      <tr><td>11:00am</td><td>11:00am</td></tr>
      <tr><td>12:00pm</td><td>12:00pm</td></tr>
      <tr><td>1:00pm</td><td>1:00pm</td></tr>
      <tr><td>2:00pm</td><td></td></tr>
    </tbody>
</table>

http://jsfiddle.net/maxcal/bhucravd/1/

答案 1 :(得分:0)

foreach($array as $row => $value){
    echo '<tr>';
    echo '<th>' . $row . '</th>';
    echo '</tr>';

    foreach($value as $x => $y){
        echo '<tr>';
        echo '<td>' . $x . '</td>';
        echo '</tr>';
    }
}

答案 2 :(得分:0)

喜欢这个?

foreach($array as $row => $value){
        echo '<th>' . $row . '</th>';
        foreach($value as $x => $y){
            echo '<tr><td>' . $x . '</td></tr>';
        }

    }

答案 3 :(得分:0)

单向,可能最简单,但不是最好的方法是使用表内的表:

echo '<table border="1"><tr>';
foreach($array as $row => $value){
    echo '<td>' . $row . "<table border="1">";
    foreach($value as $x => $y){
        echo '<tr><td>' . $x . '</td></tr>';
    }
    echo '</table></td>'
}
echo '</tr></table>';

更好的方法是&#34;转置&#34;数组,例如从旧的生成新的,然后使用foreach打印它