Echoing a multi-dimensional array in a bullet list

810 views Asked by At

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

There are 3 answers

2
Veve On BEST ANSWER

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.

4
Rizier123 On

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>";
0
Sid On

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