0

I have this array:

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
        )

)

and what I am trying to do is display this array in a table like so:

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

}

which gives me this:

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

but I am looking to display it like this:

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  

and so on for each date, would I have to change my array or my foreach? how would I fix this? I hope this makes sense.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
user979331
  • 11,039
  • 73
  • 223
  • 418

4 Answers4

1

Here is a functional approach that gives a valid HTML5 data-table.

// 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>";

Given $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
    )
);

Output (indented for readability):

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/

max
  • 96,212
  • 14
  • 104
  • 165
  • So all I need to do here is create a function, pass in my array and put this php code inside? – user979331 Nov 10 '14 at 21:32
  • Exactly, pass in your array as `$data`. I would return instead of echo the "..." string in the end (much easier to unit test). And call it by `echo date_table($data);`
    – max Nov 10 '14 at 21:40
  • A caveat is that this requires PHP 5.3+ (which is pretty old). – max Nov 10 '14 at 21:42
  • Works perfectly....I added a checkbox for each time, now I am trying to add the heading date to the checkbox value: http://stackoverflow.com/questions/26867498/php-issue-with-checkbox-value – user979331 Nov 11 '14 at 14:45
0

one way, probably simplest, but not the best is to use table inside table:

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

Better way is to "transpose" the array, e.g. to generate new one from the old one, then to print it using foreach

Nick
  • 9,962
  • 4
  • 42
  • 80
-1

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>';
    }
}
  • This won't work, since it will actually stack the columns the OP wants to create on top of eachother. – rm-vanda Nov 10 '14 at 19:31
-1

Like this?

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

    }
Pataar
  • 651
  • 8
  • 14