Reputation: 466
Let say this is my data array assigned to $my_array variable.
[0] => Array
(
[0] => 0 seconds
[1] => 05:00 PM to 6:00 PM
)
[1] => Array
(
[0] => 6 minutes
[1] => 08:00 AM to 9:00 AM
)
[2] => Array
(
[0] => 15 minutes
[1] => Array
(
[0] => 08
[1] => 15 AM
)
)
[3] => Array
(
[0] => 15 minutes
[1] => Array
(
[0] => 08
[1] => 30 AM
)
)
[4] => Array
(
[0] => 15 minutes
[1] => Array
(
[0] => 08
[1] => 45 AM
)
)
[5] => Array
(
[0] => 15 minutes
[1] => 09:00 AM to 10:00 AM
)
[6] => Array
(
[0] => 15 minutes
[1] => Array
(
[0] => 09
[1] => 15 AM
)
)
[7] => Array
(
[0] => 15 minutes
[1] => Array
(
[0] => 09
[1] => 30 AM
)
)
[8] => Array
(
[0] => 15 minutes
[1] => Array
(
[0] => 09
[1] => 45 AM
)
)
[9] => Array
(
[0] => 6 minutes
[1] => 10:00 AM to 11:00 AM
)
[10] => Array
(
[0] => 6 minutes
[1] => Array
(
[0] => 12
[1] => 30 PM
)
)
)
I have something like this:
foreach($my_array as $key => $a) {
if(!is_array($a[1)) { ?>
<div><?php echo $a; ?></div>
<?php if($key % 4 == 0) { ?>
<div class="row">
<?php } ?>
<?php } ?>
<div class="col-md-3">
<?php echo $a[0]; ?>
</div>
<?php if(!is_array($a[1)) { ?>
<?php if($key % 4 == 0) { ?>
</div>
<?php }
} ?>
}
What I want to achieve is to create a new rows with 4 columns if the second array element is not an array "if(!is_array($a[1))" and after 4 iterations close the div.
To be clear I created a static html of what I want to achieve based on data given above:
The output should be exactly like this:
Upvotes: 0
Views: 312
Reputation: 1181
I did a little cleanup of all those opening and closing PHP tags and opted to echo the bits of HTML code instead. Cleaned some syntax errors up a well.
foreach($my_array as $key => $a) {
if(!is_array($a[1])) {
echo "<div>" . $a[1] . "</div>\n";
if($key % 4 == 0) {
echo "<div class=\"row\">\n";
}
}
// create a new row div before the fourth divisible index.
// this was necessary to insert the div below the timeblock div ($a[1])
if((($key - 1) % 4) == 0) {
echo "<div class=\"row\">\n";
}
echo "\t<div class=\"col-md-3\">" . $a[0] . "</div>\n";
// added condition to close the row div on the last item
if(($key % 4 == 0) || ($key == (count($my_array) -1))) {
echo "</div>\n";
}
}
Upvotes: 2