Home » Php » html – How to print a table by given row index and column index put the Line value in table Column value in json data in php

html – How to print a table by given row index and column index put the Line value in table Column value in json data in php

Posted by: admin February 25, 2020 Leave a comment

Questions:

i am trying to print a table from json data which contain row index and column index my array is given below

[{"ID":["64103487-ab83-4ce2-ba22-e4c83184eeda"],"RowIndex":1,"ColumnIndex":1,"Line":""},{"ID":["a38cb3ca-9565-463c-830d-f686cb2a965e"],"RowIndex":1,"ColumnIndex":2,"Line":""},{"ID":["73863297-6182-4c24-a5cc-13a20f8b9b97"],"RowIndex":1,"ColumnIndex":3,"Line":"Department"},{"ID":["6c87f8d1-e7b5-49e6-8d45-049532177b0e"],"RowIndex":2,"ColumnIndex":1,"Line":""},{"ID":["ef36343e-e89a-4671-9672-310fdd451111","5e210db5-979e-4b88-9ac1-8d7d3776569c"],"RowIndex":2,"ColumnIndex":2,"Line":""},{"ID":["301021a5-9a2b-4147-85f1-def1b91c0543"],"RowIndex":2,"ColumnIndex":3,"Line":"Sales"},{"ID":["503339ef-2892-460d-9638-d08a6ebeaf5a"],"RowIndex":3,"ColumnIndex":1,"Line":"3"},{"ID":["8fff1706-8091-4ee4-9515-627e68758109","98d44dc1-a620-4de9-9eb0-4af878a46b6f"],"RowIndex":3,"ColumnIndex":2,"Line":"Justin Shore"},{"ID":["07172dfe-2037-4355-87c8-03fef616634c"],"RowIndex":3,"ColumnIndex":3,"Line":"Marketing"},{"ID":["c88a583d-f5c8-48fb-aaa8-8a1bda6bf1d5"],"RowIndex":4,"ColumnIndex":1,"Line":"4"},{"ID":["30696ecb-aabc-4735-bdf8-47f864260e26","4179cd2e-8132-4baa-afec-41d4b33870a3"],"RowIndex":4,"ColumnIndex":2,"Line":"Lexi Barringer"},{"ID":["a67c9508-7731-4953-b88e-df7dd58c6c34"],"RowIndex":4,"ColumnIndex":3,"Line":"Finance"},{"ID":["153f68af-a9aa-49a9-ab26-ad9f89e21ecc"],"RowIndex":5,"ColumnIndex":1,"Line":"5"},{"ID":["0916eb3e-e8e0-43fd-a424-bada4c95080b","66dd35de-f4d7-48e3-bb4a-a42e158c28c7"],"RowIndex":5,"ColumnIndex":2,"Line":"Safa thsan"},{"ID":["2aa280ff-b527-4b4b-9ca5-3538a3fcbb98"],"RowIndex":5,"ColumnIndex":3,"Line":"Sales"},{"ID":["3d9af0ca-e0e8-4747-bfc7-85273a053a0f"],"RowIndex":6,"ColumnIndex":1,"Line":"6"},{"ID":["99314a19-0377-416d-bc25-6efcf4943abb","d001dfb2-d51c-460d-bbd7-d2340695dddb"],"RowIndex":6,"ColumnIndex":2,"Line":"Roy Flint"},{"ID":["66201bf3-aa64-4761-a291-040310b1b5cb"],"RowIndex":6,"ColumnIndex":3,"Line":"Finance"}]

and i want to ouput like the image given below

enter image description here

How to&Answers:
<?php 
$data = file_get_contents('test.json');
$data =json_decode($data,TRUE);
 $dataA= array();
foreach($data as $singleArray){
array_push($dataA, $singleArray['Line']);

}
$arrayChunks= array_chunk($dataA, 3);

?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<table>
    <thead>
        <th>Names</th>
        <th>Department</th>

    </thead>
    <tbody>
       <?php 
            foreach($arrayChunks as $array){
                if(empty($array[0]) || empty($array[1]) || empty($array[2]))continue;
       ?>
        <tr>
            <td><?php echo $array[1]; ?></td>
            <td><?php echo $array[2]; ?></td>


        </tr>
        <?php 
        }

        ?>
    </tbody>
</table>

</body>
</html>

Hi Its exactly what you are looking. try to check this and setup output as you need. Its just idea you can surely improve your problem using this. Thanks

Answer:

This is what i want
result as image given in question

    $data = json_decode($data , true);
$k = 1;
$l=1;
$html .='<table class="table table-striped table-bordered" border="1">';
foreach($data as $combine){
    if($combine['RowIndex']==$k){
         $html .='<tr>';
         foreach($data as $com){
             if($com['RowIndex']==$l){
                $html .='<td>'.$com['Line'].'</td>';
             }
         }
    $html .='</tr>';
    //$k=$combine['RowIndex'];
    $k++;
    $l++;
    }
}
$html .='</table>';
print_r($html);
die;