Reputation: 3665
I have the following array which came up from my two days work with arrays.
Array (
[0]=> Array(
[6]=>Array(
[English] => 17
[Maths] => 11
[Science] => 15
)
)
[1] =>Array(
[7]=>Array(
[English] => 13
[Maths] => 15
[Science] => 11
)
)
[2] =>Array(
[8]=>Array(
[English] => 9
[Maths] => 17
[Science] => 9
)
)
)
Here array key 6,7, and 8 are unique. I want to print out this array in this format.
<table border="1">
<thead>
<tr>
<th>Roll No</th>
<th>English</th>
<th>Maths</th>
<th>Science</th>
</tr>
</thead>
<tbody>
<tr>
<td>6</td>
<td>17</td>
<td>11</td>
<td>15</td>
</tr>
<tr>
<td>7</td>
<td>13</td>
<td>15</td>
<td>11</td>
</tr>
<tr>
<td>8</td>
<td>9</td>
<td>17</td>
<td>9</td>
</tr>
</tbody>
</table>
Note: the header and the rows are dynamic.
Upvotes: 0
Views: 2009
Reputation: 1057
Please try this,
<table>
<thead>
<tr>
<th>Roll No</th>
<?php foreach($arrays as $k1=>$val1): ?>
<?php foreach($val1 as $k2=>$val2): ?>
<?php foreach($val2 as $k3=>$val3): ?>
<th><?=$k3?></th>
<?php endforeach; ?>
<?php break; endforeach; ?>
<?php break; endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach($arrays as $k1=>$val1): ?>
<tr>
<?php foreach($val1 as $k2=>$val2): ?>
<th><?=$k2?></th>
<th><?=$val2['English']?></th>
<th><?=$val2['Maths']?></th>
<th><?=$val2['Science']?></th>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As you have little complex array
, we have to run foreach
2 time, 1st to generate <tr>
3 time and 2nd to display records.
For th
we have to make roll number
static because we have no strong in array for that.
Upvotes: 1
Reputation: 846
Given this array structure (taken from your question, put into code):
$rows = array(
array(
6 => array(
'English' => 17,
'Maths' => 11,
'Science' => 15
)
),
array(
7 => array(
'English' => 13,
'Maths' => 15,
'Science' => 11
)
),
array(
8 => array(
'English' => 9,
'Maths' => 17,
'Science' => 9
)
)
);
First, get all the subjects (English, Maths, Science, etc) that need to be displayed:
$columns = array();
foreach ($rows as $row) {
foreach ($row as $roll => $set) {
foreach ($set as $class => $score) {
$columns[$class] = $class;
}
}
}
Then, display them (quick and dirty):
<table border="1">
<thead>
<tr>
<th>Roll No</th>
<?php
foreach ($columns as $column) {
echo '<th>' . $column . '</th>';
}
?>
</tr>
</thead>
<tbody>
<?php
foreach ($rows as $row) {
echo '<tr>';
foreach ($row as $roll => $set) {
echo '<td>' . $roll . '</td>';
foreach ($columns as $class) {
echo '<td>' . ((array_key_exists($class, $set)) ? $set[$class] : 'n/a') . '</td>';
}
}
echo '</tr>';
}
?>
</tbody>
</table>
Upvotes: 1
Reputation: 92854
"Quick" solution with array_walk
, array_merge
, array_values
and array_keys
functions:
// assuming that $arr is your initial array
$header_sequence = ['Science','Maths','English']; // if header's key sequence is dynamic
sort($header_sequence);
$result = [];
array_walk($arr, function($v, $k) use (&$result){
$key = array_keys($v)[0];
$result[$k] = array_merge([$key], array_values($v[$key]));
});
?>
<table>
<thead>
<tr>
<th>Roll No</th>
<?php foreach($header_sequence as $item): ?>
<th><?= $item ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach($result as $k => $v): ?>
<tr>
<?php foreach($v as $item): ?>
<td><?= $item ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
The output:
Roll No English Maths Science
6 17 11 15
7 13 15 11
8 9 17 9
Upvotes: 1