0

I met problem, that, I believe, you can help me with.

I have next array:

JSON:

{
"1":{
"01:00":"1",
"02:00":""
},
"2":{
"01:00":"3",
"02:00":""
},
"3":{
"01:00":"23",
"02:00":""
},
"4":{
"01:00":"234",
"02:00":""
},
"5":{
"01:00":"234",
"02:00":""
},
"6":{
"01:00":"",
"02:00":""
},
"7":{
"01:00":"",
"02:00":""
}
}

PHP:

array (
    1 => array ( '01:00' => '1', '02:00' => '', ),
    2 => array ( '01:00' => '3', '02:00' => '', ),
    3 => array ( '01:00' => '23', '02:00' => '', ),
    4 => array ( '01:00' => '234', '02:00' => '', ),
    5 => array ( '01:00' => '234', '02:00' => '', ),
    6 => array ( '01:00' => '', '02:00' => '', ),
    7 => array ( '01:00' => '', '02:00' => '', ),
)

I am trying to put right values to right places, but I am at dead end :(

Can you help me with the table generator for this array.

It should look like this with values in inputs: enter image description here

First line is quite static:

    echo "<table><tr>";
    for ($i = 0; $i < 8; $i++) {
        echo "<td>$days[$i]</td>";
    }
    echo "</tr>";

And then problems starts.

1
  • This is asking you to change quite a bit so, just a comment: $days = array ( '01:00' => array ('1','3','23','234','234','',''), '02:00=> array ('','','','','','','')); and than foreach ($days as $hours) { echo "<table><tr><td>" . implode("</td><td>", $hours) . "</td></tr></table>"; } Commented Sep 16, 2015 at 0:22

3 Answers 3

2

try the following code

<?php
$days = array (
    1 => array ( '01:00' => '1', '02:00' => '', ),
    2 => array ( '01:00' => '3', '02:00' => '', ),
    3 => array ( '01:00' => '23', '02:00' => '', ),
    4 => array ( '01:00' => '234', '02:00' => '', ),
    5 => array ( '01:00' => '234', '02:00' => '', ),
    6 => array ( '01:00' => '', '02:00' => '', ),
    7 => array ( '01:00' => '', '02:00' => '', ),
);

echo "<table border='1'><thead><th>0</th>";

foreach($days as $k=>$v){   
    echo "<th>".$k."</th>"; 
}
echo "</thead><tbody>";

$rows = array_keys($days[1]);//edited

foreach($rows as $row){
    echo "<tr><td>".$row."</td>";
    foreach($days as $key=>$val){
        echo "<td>";
        echo "<input type='text' name='txt".$key."' value='".$days[$key][$row]."' style='width:40px;' />";//edited
        echo "</td>";
    }
    echo "</tr>";
}

echo "</tbody></table>";


?>
Sign up to request clarification or add additional context in comments.

3 Comments

kudos for putting in the extra effort, but you forgot to give the inputs a value :P, this just makes a table of empty inputs.
lol i'm not the asker. I posted a competing answer, actually. just admiring your work :P
poster isn't notified of your comments until he comments on your post. If you want to ask him you should comment on the question, not on your own answer.
1

Remember, you've got an array of arrays. When you loop through the array, you then need to loop through the inner array.

Personally, I like while loops, and if you don't mind doing it in reverse, this example should help.

function makeTableFromArray($array){
    $array = array_values($array);
    $tbl = "<table>";
    $i = count($array);
    while($i--){
        $tbl .= "<tr>";
        $row = array_values($array[$i]);
        $n = count($row);
        while($n--){
            $tbl .= "<td>".$row[$n]."</td>";
        }
        $tbl .= "</tr>";
    }
    $tbl .= "</table>";
    return $tbl;
}

Here's an example: https://3v4l.org/L5Wfu

Edit if you don't like doing it in reverse, you can reverse the array first: makeTableFromArray(array_reverse($array));

Comments

1

What you could do is loop over the y and x axes of the table and pull the value from the array.

$array = array (
    1 => array ( '01:00' => '1', '02:00' => '', ),
    2 => array ( '01:00' => '3', '02:00' => '', ),
    3 => array ( '01:00' => '23', '02:00' => '', ),
    4 => array ( '01:00' => '234', '02:00' => '', ),
    5 => array ( '01:00' => '234', '02:00' => '', ),
    6 => array ( '01:00' => '', '02:00' => '', ),
    7 => array ( '01:00' => '', '02:00' => '', ),
);

echo "<table>\n";
for ($y = 0; $y < count(reset($array)); $y++) {
    echo "\t<tr>\n";
    for ($x = 0; $x <= count($array); $x++) {
        echo "\t\t<td>";
        if ($y == 0) {
            echo $x;
        }
        elseif (isset($array[$x])) {
            echo $array[$x]["0$y:00"];
        }
        else {
            echo "0$y:00";
        }
        echo "</td>\n";
    }
    echo "\t</tr>\n";
}
echo "\n</table>";

Here is the output:

<table>
    <tr>
        <td>0</td>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
        <td>5</td>
        <td>6</td>
        <td>7</td>
    </tr>
    <tr>
        <td>01:00</td>
        <td>1</td>
        <td>3</td>
        <td>23</td>
        <td>234</td>
        <td>234</td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td>02:00</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>

</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.