duanemei2194
2014-11-22 19:35 阅读 23
已采纳

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

I think I have my array correctly inputted but echoing it out to put it into a table is being difficult. I don't know where I am wrong.

Here is my code:

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

I am good up until the bullet point for each day. Then I have my echo code:

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条回答 默认 最新

  • 已采纳
    dongmo2324 dongmo2324 2014-11-22 19:44

    You're mixing table elements (<th>, <td>) with list ones (<ul>, <li>). Here is how you can do what you want as a list :

    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>';
    

    EDIT : the same as a row of your table

    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 : added bullet points.

    点赞 评论 复制链接分享
  • dongyi9484 dongyi9484 2014-11-22 19:40

    I think your looking for something like this:

    echo '<ul>';
    
        foreach($thingsToDo as $innerArray) {
            echo "<li>";
    
            foreach($innerArray as $k => $v)
                echo $v . "<br />";
    
            echo "</li>";
        }
    
    echo '</ul>';
    

    Or if you want ever element to be a todo point use this:

    echo "<ul>";
    
        foreach($thingsToDo as $innerArray) {
    
            foreach($innerArray as $k => $v)
                echo "<li>" . $v . "</li>";
    
    
        }
    
    echo "</ul>";
    
    点赞 评论 复制链接分享
  • douqiang7462 douqiang7462 2014-11-22 20:08

    I hope below code serves your purpose:

    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>';
    
    点赞 评论 复制链接分享