Output a PHP Multi-Dimensional Array to a HTML Table

PHP multidimensional array to html table

You've got one too many foreach's going on there. Try this instead:

echo "<table><tr><td>Question</td><td>Rating</td></tr>";
foreach ($marksarray as $mks){
echo "<tr><td>".$mks[0]."</td><td>".$mks[1]."</td></tr>";
}
echo "</table></div>";

For future reference, it makes your code far easier to understand if you use an array of associative arrays with meaningful keys. e.g.

$marksarray = array(
array('qid' => 8, 'rating' => 0),
array('qid' => 9, 'rating' => 1),
array('qid' => 13, 'rating' => 2)
);

Then your loop would look like this:

foreach ($marksarray as $mark){
echo "<tr><td>".$mark['qid']."</td><td>".$mark['rating']."</td></tr>";
}

Better still, you should use MVC (Model, View, Controller) and pass this data into a view...but that's another subject entirely.

How to display the PHP multiple dimensional associative array in html table?

  • First of all get the headlines
  • Then iterate thru the schools
  • Output the columns with their contents
echo "<table>\n";
echo "<tr>";
foreach (array_keys(reset($city['schoolDetails'])) as $headline) {
echo "<th>$headline</th>";
}
echo "</tr>\n";

foreach ($city['schoolDetails'] as $school) {
echo "<tr>";
displayColumn($school);
echo "</tr>\n";
}
echo "</table>\n";

function displayColumn(array $array)
{
foreach ($array as $key => $value) {
echo "<td>";
if (is_array($value)) {
echo implode("<br>\n", $value);
} else {
echo $value;
}
echo "</td>";
}
}

ResultTable

How to convert multidimensional PHP array to html table

You can use array_merge and foreach for the desired output

$arr = array_merge([ 0 => ['ID','FIRST NAME','LAST NAME','CITY','PHONE']],$arr);
$html = '<table border="1">';
foreach($arr as $row){
$html .= '<tr>';
foreach($row as $column){
$html .= '<td>'.$column.'</td>';
}
$html .= '</tr>';
}
$html .= '</table>';
echo $html;

Print multidimensional array to table

Here is the code you should try,

foreach ($final_array as $food_array) {
echo '<tr>';
foreach ($food_array as $key1 => $value1) {
echo '<td>' . $value1['label'] . '</td>';
}
echo '<tr>';
}

You need to loop it twice to get your output.

Multi-Dimensional PHP Array Into HTML Table

I hope below code may help you

<?php
$dns = dns_get_record("stackoverflow.com");
?>

<table>
<tr>
<th>Host</th>
<th>Type</th>
<th>Class</th>
<th>TTL</th>
<th>Extra</th>
</tr>
<?php
foreach($dns as $dnsset) {
?>
<tr>
<td><?php echo $dnsset["host"] ?></td>
<td><?php echo $dnsset["type"] ?></td>
<td><?php echo $dnsset["class"] ?></td>
<td><?php echo $dnsset["ttl"] ?></td>
<td>
<?php
foreach($dnsset as $extrasetkey => $extrasetvalue) {
if(!in_array($extrasetkey, ['host', 'type', 'class', 'ttl'])) {
// If your data has extrasetvalue as array type also add conditions
// here and loop through them also
echo $extrasetkey . ":" . $extrasetvalue . "<br/>";
}
}
?>
</td>
</tr>
<?
}
?>
</table>

Output a php multi-dimensional array to a html table

In your HTML, try something like this:

<table>
<tr>
<th>Bottom</th>
<th>Slant</th>
<th>Fitter</th>
</tr>
<?php foreach ($_POST['order'] as $order): ?>
<tr>
<td><?php echo $order['bottomdiameter'] ?></td>
<td><?php echo $order['slantheight'] ?></td>
<td><?php echo $order['fittertype'] ?></td>
</tr>
<?php endforeach; ?>
</table>

Obviously, I'm not including all your attributes there, but hopefully you get the idea.

Access a value from a multidimensional Array to display in HTML table using foreach/for loop

What you are doing is, you are looping the array twice, thus going 2 times in.
On the first iteration $data would be

Array(
[0] => Array(
[Name] => Lot Survey Plans and Specifications
[Document] => -
[Id] => 10
[Is_Received] => 0
[Is_Personal_Submission] => 1
)
[1] => Array(
[Name] => Others (Specify)
[Document] => -
[Id] => 11
[Is_Received] => 0
[Is_Personal_Submission] => 1
)
)

On the first iteration of the 2nd foreach, $values would be

Array(
[Name] => Lot Survey Plans and Specifications
[Document] => -
[Id] => 10
[Is_Received] => 0
[Is_Personal_Submission] => 1
)

UPDATE:

Your code should look something like

<?php 
foreach($appreq as $row => $data):
// table name print
// table headers print
foreach ($data as $key => $values) :
//print row content by calling $values['Is_Received'], $values['Name'], ...
endforeach;
endforeach
?>

I hope this answers your question.
Please, let me know if there is anything else unclear.

UPDATE 2

<?php 
foreach($appreq as $row => $value) : ?>

<table style="text-align: left; width: 50%">
<thead>
<tr>
<th scope="col">#</th>
<th width='10' style="text-align:center;">Received</th>
<th scope="col">Requirement</th>
</tr>
</thead>
<tbody>
<h5><strong><?= $ancillarypermit[$row]->permit->Name ?></strong></h5>
<?php
foreach ($value as $key => $values) :
?>
<tr>
<th scope="row">1</th>
<td width='150' style='text-align: center; vertical-align: middle; padding: 3px'>
<?php
echo CheckboxX::widget([
'name'=>'s_1',
'value'=>$values['Is_Received'],
// 'readonly'=>true,
'pluginOptions'=>['threeState'=>false]
]);
?>
</td>
<td> <?= $values['Name'] ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endforeach;?>

Multidimensional Array as html-table entries per day among themselves

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>

Update

Refering to your comment making them Links you could change to

echo "<tr>\n";
foreach($notes as $note) {
printf("<td><a href='edit.php?id=%s'>%s</a></td>", urlencode($note), $note);
}
echo "</tr>\n";

How do I echo multidimensional array values by table rows instead of columns in php

<?php 
$array = Array (
Array (
'Paracetamol',
'Caffeine',
'Pase'
),
Array (
12,
10,
1
),
Array (
'Packets',
'Cartons',
'Containers'
)
);

function combine($array) {
array_unshift($array, null);
return call_user_func_array('array_map', $array);
}

$array = combine($array);
?>
<table border="1">
<tr>
<th>Product_name</th>
<th>Product_quantity</th>
<th>Product_size</th>
</tr>
<?php foreach($array as $row): ?>
<tr>
<td><?=$row[0]?></td>
<td><?=$row[2]?></td>
<td><?=$row[1]?></td>
</tr>
<?php endforeach ?>

</table>

Printing a multidimensional array in php into an html table

Try :

echo '<table><tr><th>Id</th><th>Name</th></tr>';
foreach ($response['sample'] as $player) {
echo '<tr><td>'.$player->id.'</td><td>'.$player->name.'</td></tr>';
}
echo '</table>';


Related Topics



Leave a reply



Submit