回应子弹列表中的多维数组

时间:2014-11-22 19:35:41

标签: php arrays html-table bulletedlist

我想我已经正确输入了我的阵列,但回应它将它放入表中很困难。我不知道我错在哪里。

这是我的代码:

<?php
error_reporting(E_ALL);
//days - Sunday thru Saturday
$daysOfTheWeek = array(0 => 'Sunday', 1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday');

//sleep schedule 
$sleepSchedule = array( 0 => 'go to sleep early', 1 => 'go to sleep on time', 2 => 'go to sleep on time', 3 => 'go to sleep on time',
               4 => 'go to sleep on time', 5 => 'stay up late', 6 => 'stay up late');

//the chore list 
$thingsToDo = array(
  0 => array('Cleanup the house', 'Do dishes', 'Weekly Quiz!'), //row 1
  1 => array('Grocery shop'), //row 2
  2 => array('Walk at the park', 'Read the weekly PHP chapter', 'Take out the food!', 'Cook dinner!'), //row 3
  3 => array('Homework due!', 'Ice cream sundae!'), //row 4
  4 => array('Walk the dog'), //row 5
  5 => array('Excercise', 'Vist Family'), //row 6
  6 => array('Watch my favorite Tv show', 'Shopping!', 'Make take out food!', 'Relax!!') //row 7 
);

    echo '<tr>';
    echo '<th>Days of the week</th>';
    for ($i = 0; $i < sizeof($daysOfTheWeek); $i++) { 
    echo "<td>{$daysOfTheWeek[$i]}</td>";
    }
    echo '</tr>';

    echo '<tr>';
    echo '<th>Sleep Schedule</th>';
    for ($i = 0; $i < sizeof($sleepSchedule); $i++) { 
    echo "<td>{$sleepSchedule[$i]}</td>";
    }


$thingsToDo = array(
  0 => array('Cleanup the house', 'Do dishes', 'Weekly Quiz!'), //row 1
  1 => array('Grocery shop'), //row 2
  2 => array('Walk at the park', 'Read the weekly PHP chapter', 'Take out the food!', 'Cook dinner!'), //row 3
  3 => array('Homework due!', 'Ice cream sundae!'), //row 4
  4 => array('Walk the dog'), //row 5
  5 => array('Excercise', 'Vist Family'), //row 6
  6 => array('Watch my favorite Tv show', 'Shopping!', 'Make take out food!', 'Relax!!') //row 7 
);

我很好,直到每天的要点。然后我有我的回音代码:

echo '<th>ToDo list</th>';
    $sizeOfThingsToDo = sizeof($thingsToDo);
    for ($i = 0; $i < $sizeOfThingsToDo; $i++) {
    $list = $thingsToDo[$i];

echo '<ul><td><li>';

 echo $list[0].'<br>';
 echo $list[1].'<br>';
 echo $list[2].'<br>';
 echo $list[3].'<br>'; echo '</ul></td></li>';

3 个答案:

答案 0 :(得分:1)

您将表格元素(<th><td>)与列表元素(<ul><li>)混合在一起。以下是如何按列表执行所需操作:

echo '<b>ToDo list</b>';
$sizeOfThingsToDo = sizeof($thingsToDo);
echo '<ul>';
for ($i = 0; $i < $sizeOfThingsToDo; $i++) {
    $list = $thingsToDo[$i];
    echo '<li>List #'.$i.':';
    echo '    <ul>';
    $sizeOfList = sizeof($list);
    for ($j = 0; $j < $sizeOfList; $j++) {
        echo '        <li>'.$list[$j].'</li>';
    }
    echo '    </ul>';
    echo '</li>';
}
echo '</ul>';

编辑:与表格中的一行相同

echo '<tr><th>ToDo list</th>';
$sizeOfThingsToDo = sizeof($thingsToDo);
for ($i = 0; $i < $sizeOfThingsToDo; $i++) {
    $list = $thingsToDo[$i];
    echo '    <td>';
    echo '        <ul>';
    $sizeOfList = sizeof($list);
    for ($j = 0; $j < $sizeOfList; $j++) {
        echo '<li>'.$list[$j].'</li>';
    }
    echo '        </ul>';
    echo '    </td>';
}
echo '</tr>';

REEDIT:增加了要点。

答案 1 :(得分:-1)

我认为您正在寻找类似的东西:

echo '<ul>';

    foreach($thingsToDo as $innerArray) {
        echo "<li>";

        foreach($innerArray as $k => $v)
            echo $v . "<br />";

        echo "</li>";
    }

echo '</ul>';

或者,如果您希望元素成为待用点,请使用:

echo "<ul>";

    foreach($thingsToDo as $innerArray) {

        foreach($innerArray as $k => $v)
            echo "<li>" . $v . "</li>";


    }

echo "</ul>";

答案 2 :(得分:-1)

我希望以下代码符合您的目的:

echo '<b>ToDo list</b>';
$sizeOfThingsToDo = sizeof($thingsToDo);
echo '<table><tr><th>Day Of Week</th><th>Things To Do</th></tr>';
foreach($thingsToDo as $dayNumber => $listThings)
{
    echo '<tr>';
    echo '<td>'.$daysOfTheWeek[$dayNumber ].'</td><td><ul>';
    foreach($listThings as $thingDetails)
    {
        echo '<li>'.$thingDetails.'</li>';
    }
    echo '</ul></td></tr>';

}
echo '</table>';