I am not really sure that this issue is about splinting an array. I am running into a wall because i cant seem to find the best way to do this.
I have a mutlydimetional array that looks like this.
Array ( [0] => )
[20] => Array ( [0] => Time[s] [1] => B2 [2] => F3 [3] => C6 [4] => F7 )
[21] => Array ( [0] => 0.0 [1] => 57.68 [2] => 58.83 [3] => 58.95 [4] => 59.11 )
[22] => Array ( [0] => 0.5 [1] => 58.12 [2] => 59.21 [3] => 59.24 [4] => 59.43 )
This array can go on for thousands of arrays. It is a temperature reading for multiple point every few second.
What I am trying to do is split it in multiple pages or multiple tables but iterating in the same code so that i can use the same background or style table.
Table 1 Results 1 - 10
1
2
3
4
5
..
Table 2 Results 11 - 21
11
12
13
14
15
......
And so on.
So fart I have this. But it only seems to give me the entire list. No matter what i tried I cant seems to figure out how to split it. Any help will be appreciate it.
echo '<table border="1">';
echo '<tbody>';
for ($row = 20; $row < $rows; $row++) {
echo '<tr>';
for ($col = 0; $col < 15; $col++) {
echo "<td>".$csvarray[$row][$col]."</td>";
}
echo '</tr>';
}
echo '</tbody></table>';
You'd probably need another loop:
for ($i = 0; $i < $max_rows; $i++) {
echo "<p>Table " . ($i + 1) . "</p><table>";
for ($j = ($i+1)* 20; ...) {
echo "<tr>";
for ($k = 0; $k < 15; $k++) {
echo "<td> .... </td>";
}
echo "</tr>";
}
echo "</table>";
}
The $i
loop creates your Table X
headings. $j
loop handles building the rows, $k
loop handles building the columns.
You can use array_slice()
function. It's like a substr function for arrays.
Ok so In the end i used a combination of all the answers here is the result in case somebody is looking for the same question.
$csvarray = array_chunk($csvarray, 20);
$tables = count($csvarray);
for ($table = 1; $table < $tables; $table++) {
echo '<table border="1">';
echo '<tbody>';
echo $table + 1;
$rows = count($csvarray[$table]);
for ($row = 0; $row < $rows; $row++) {
$vals = count($csvarray[$table][$row]);
echo '<tr>';
for ($val = 0; $val < $vals; $val++) {
echo "<td>".$csvarray[$table][$row][$val]."</td>";
}
echo '</tr>';
}