Reputation: 1
I'm trying to reverse the order of images that are displayed. I'm a PHP noob and I'm not sure how to do it. I guess I need to reverse the order of foreach that is displayed, but I'm not entirely sure how to do that.
<div class="yacht-view-right">
<?php
if (count($tpl['gallery_arr']) > 0)
{
$is_open = false;
foreach ($tpl['gallery_arr'] as $k => $v)
{
if ($k == 0)
{
$size = getimagesize(BASE_PATH . $v['medium_path']);
?>
<p><?php print_r(array_keys($v));
print_r(array_VALUES($v));
echo (count($tpl['gallery_arr']))
?></p>
<div class="yacht-view-pic" id="yacht-view-pic" style="width:<?php echo $size[0]; ?>px; height: <?php echo $size[1]; ?>px;">
<img id="yacht-view-medium-pic" src="<?php echo BASE_PATH . $v['medium_path']; ?>" alt="<?php echo htmlspecialchars(stripslashes($v['title'])); ?>"/> </a>
</div>
<?php
}h
$is_open = true;
?>
<div class="yacht-view-img">
<a href="<?php echo BASE_PATH . $v['large_path']; ?>" data-lightbox="yachts">
<img src="<?php echo BASE_PATH . $v['small_path']; ?>" alt="<?php echo htmlspecialchars(stripslashes($v['title'])); ?>" />
</a>
</div>
<?php
/*if ($k > 0 && ($k + 1) % 4 === 0)
{
$is_open = false;
?><div class="clear_left"></div><?php
}*/
}
if ($is_open)
{
?>
<div class="clear_left"></div>
<?php
}
} else {
}
?>
Upvotes: 0
Views: 70
Reputation: 101
So it looks like you could use the array_reverse()
function
$tpl['gallery_arr'] = array_reverse( $tpl['gallery_arr'], true );
foreach ($tpl['gallery_arr'] as $k => $v){
...
}
Or use a regular for
loop with inverted parameters.
for($k = count($tpl['gallery_arr']) - 1; $k >= 0; $k--){
$v = $tpl['gallery_arr'][$k];
...
}
Upvotes: 0
Reputation: 141839
You could simply reverse the order of the array using array_reverse before iterating through it with foreach:
foreach ( array_reverse( $tpl['gallery_arr'] ) as $k => $v)
or you could iterate in reverse using a counter (which may have slightly better performance, if the array is large):
$gallery = $tpl['gallery_arr'];
for($i = $first = count($gallery) - 1; $i >= 0; $i-- ) {
$v = $gallery[$i];
if ( $k == $first ) {
...
}
Upvotes: 0
Reputation: 8595
You can simply use array_reverse()
before starting your foreach
iteration:
$is_open = false;
$tpl['gallery_arr'] = array_reverse( $tpl['gallery_arr'], true );
foreach ($tpl['gallery_arr'] as $k => $v)
Upvotes: 2