Make use of the modulo operator, which calculates the remainder of the division of two numbers. Modulo operations are often used as solutions for processes which require repetitions with iteration steps bigger than 1 and in situations were chunking big data bundles is needed. Example: when you want to send 100 emails at once, then you loop through them and send them in packages of max. 10 per iteration step.
The principle applied in your case is: Loop through the source array and output a result (two table rows) each second iteration step, beginning with the first array key.
Version 1 - Output directly in HTML:
<?php
/*
* Original array.
*/
$source = [
0 => [
'Title' => 'Title 1',
'Value' => 'Value 1'
],
1 => [
'Title' => 'Title 2',
'Value' => 'Value 2'
],
2 => [
'Title' => 'Title 3',
'Value' => 'Value 3'
],
3 => [
'Title' => 'Title 4',
'Value' => 'Value 4'
],
4 => [
'Title' => 'Title 5',
'Value' => 'Value 5'
],
];
echo 'ORIGINAL:<pre>' . print_r($source, true) . '</pre>';
/*
* Filter array for empty/null values.
* The result is another array.
*/
$source = array_filter($source, function($el) {
return !(
empty($el['Title']) ||
!isset($el['Title']) ||
empty($el['Value']) ||
!isset($el['Value'])
);
});
echo 'FILTERED:<pre>' . print_r($source, true) . '</pre>';
/*
* Reset array keys by applying array_values().
* The result is another array.
*/
$source = array_values($source);
echo 'ARRAY_VALUES:<pre>' . print_r($source, true) . '</pre>';
?>
<table>
<?php
foreach ($source as $key => $item) {
if ($key % 2 == 0) {
?>
<tr>
<td>
<?php
if (isset($source[$key]['Title'])) {
echo $source[$key]['Title'];
}
?>
</td>
<td>
<?php
if (isset($source[$key + 1]['Title'])) {
echo $source[$key + 1]['Title'];
}
?>
</td>
</tr>
<tr>
<td>
<?php
if (isset($source[$key]['Value'])) {
echo $source[$key]['Value'];
}
?>
</td>
<td>
<?php
if (isset($source[$key + 1]['Value'])) {
echo $source[$key + 1]['Value'];
}
?>
</td>
</tr>
<?php
}
}
?>
</table>
Version 2 - Output from PHP:
<?php
/*
* Original array.
*/
$source = [
0 => [
'Title' => 'Title 1',
'Value' => 'Value 1'
],
1 => [
'Title' => 'Title 2',
'Value' => 'Value 2'
],
2 => [
'Title' => 'Title 3',
'Value' => 'Value 3'
],
3 => [
'Title' => 'Title 4',
'Value' => 'Value 4'
],
4 => [
'Title' => 'Title 5',
'Value' => 'Value 5'
],
];
echo 'ORIGINAL:<pre>' . print_r($source, true) . '</pre>';
/*
* Filter array for empty/null values.
* The result is another array.
*/
$source = array_filter($source, function($el) {
return !(
empty($el['Title']) ||
!isset($el['Title']) ||
empty($el['Value']) ||
!isset($el['Value'])
);
});
echo 'FILTERED:<pre>' . print_r($source, true) . '</pre>';
/*
* Reset array keys by applying array_values().
* The result is another array.
*/
$source = array_values($source);
echo 'ARRAY_VALUES:<pre>' . print_r($source, true) . '</pre>';
$tableRows = '';
foreach ($source as $key => $item) {
if ($key % 2 == 0) {
$tableRows .= sprintf('
<tr>
<td>
%s
</td>
<td>
%s
</td>
</tr>
<tr>
<td>
%s
</td>
<td>
%s
</td>
</tr>
'
, isset($source[$key]['Title']) ? $source[$key]['Title'] : ''
, isset($source[$key + 1]['Title']) ? $source[$key + 1]['Title'] : ''
, isset($source[$key]['Value']) ? $source[$key]['Value'] : ''
, isset($source[$key + 1]['Value']) ? $source[$key + 1]['Value'] : ''
);
}
}
echo sprintf('<table>%s</table>', $tableRows);