Home > Enterprise >  Multidimensional Array as html-table entries per day among themselves
Multidimensional Array as html-table entries per day among themselves

Time:07-05

is there a easy way to build from this multidimensional array a HTML-Table in this Format:

Monday Tuesday Wednesday Thursday Friday Saturday Sunday
Otto Monday Fritz Tuesday Christine Thursday Otto Saturday Otto Sunday
Anna Monday
$calendar["Monday"][] = "Otto Monday";
$calendar["Monday"][] = "Anna Monday";
$calendar["Tuesday"][] = "Fritz Tuesday";
$calendar["Wednesday"][] = "";
$calendar["Thursday"][] = "Christine Thursday";
$calendar["Friday"][] = "";
$calendar["Saturday"][] = "Otto Saturday";
$calendar["Sunday"][] = "Otto Sunday";

I have this:

<table>
    <tr>
    //Make the table header
    <?php 
    foreach ($calendar as $printout) {
        echo "<th>" . key($calendar) ."</th>";
next($calendar);
    } 
    ?>
    </tr>
</table>

If a want the Names in one row, I understand, but what is the code when I sort the names per day among themselves?

CodePudding user response:

You can achieve this by looping through the array.

$calendar["Monday"][] = "Otto Monday";
$calendar["Monday"][] = "Anna Monday";
$calendar["Tuesday"][] = "Fritz Tuesday";
$calendar["Wednesday"][] = "";
$calendar["Thursday"][] = "Christine Thursday";
$calendar["Friday"][] = "";
$calendar["Saturday"][] = "Otto Saturday";
$calendar["Sunday"][] = "Otto Sunday";

$daynames = array_unique(array_keys($calendar));
echo "<table>\n<tr>\n";
foreach($daynames as $dayname) {
    echo "<th>$dayname</th>\n";
}
echo "</tr>\n";

$rownumber = 0;
do {
    $count = 0;
    $notes = [];
    foreach ($daynames as $dayname) {
        $note = $calendar[$dayname][$rownumber] ?? '';
        if($note) {
            $count  ;
            $notes[] = $note;
            continue;
        }
        $notes[] = '';
    }
    if($count > 0) {
        echo "<tr>\n", join('', array_map(fn($note) => "<td>$note</td>", $notes)), "</tr>\n";
        $rownumber  ;
    }
} while($count > 0);

echo "</table>\n";

will give

<table>
<tr>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
<th>Sunday</th>
</tr>
<tr>
<td>Otto Monday</td>
<td>Fritz Tuesday</td>
<td></td>
<td>Christine Thursday</td>
<td></td>
<td>Otto Saturday</td>
<td>Otto Sunday</td>
</tr>
<tr>
<td>Anna Monday</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>

CodePudding user response:

$calendar['Monday'] = [ 'Otto Monday', 'Anna Monday' ];
$calendar['Tuesday'] = [ 'Fritz Tuesday' ];
$calendar['Wednesday'] = [];
$calendar['Thursday'] = [ 'Christine Thursday', 'More Thursday', 'Third Thursday' ];
$calendar['Friday'] = [];
$calendar['Saturday'] = [ 'Otto Saturday' ];
$calendar['Sunday'] = [ 'Otto Sunday' ];

$countOfRows = max(array_map(fn($item) => count($item), $calendar));

for ($index = 0; $index < $countOfRows; $index  ) {
  foreach ($calendar as $day => $items) {
    $rows[$index][$day] = ( array_key_exists($index, $items) ) ? $items[$index] : '';
  }
}

print_r($rows);

Output:

Array
(
    [0] => Array
        (
            [Monday] => Otto Monday
            [Tuesday] => Fritz Tuesday
            [Wednesday] => 
            [Thursday] => Christine Thursday
            [Friday] => 
            [Saturday] => Otto Saturday
            [Sunday] => Otto Sunday
        )

    [1] => Array
        (
            [Monday] => Anna Monday
            [Tuesday] => 
            [Wednesday] => 
            [Thursday] => More Thursday
            [Friday] => 
            [Saturday] => 
            [Sunday] => 
        )

    [2] => Array
        (
            [Monday] => 
            [Tuesday] => 
            [Wednesday] => 
            [Thursday] => Third Thursday
            [Friday] => 
            [Saturday] => 
            [Sunday] => 
        )

)

And the HTML can then be generated like this:

$thead = '<thead><tr>';
foreach(array_keys($calendar) as $day) {
  $thead .= "<th>$day</th>";
}
$thead .= '</tr></thead>';

$tbody = '<tbody>';
foreach($rows as $row) {
  $tbody .= "<tr>";
  foreach($row as $value) {
    $tbody .= "<td>$value</td>";
  }
  $tbody .= "</tr>";
}

$table = "<table>$thead$tbody</table>";

echo $table;
  • Related