试图在PHP中反转图像顺序

时间:2015-07-04 04:21:43

标签: php

我正在尝试颠倒显示的图像的顺序。我是一个PHP菜鸟,我不知道该怎么做。我想我需要扭转显示的foreach的顺序,但我不完全确定如何做到这一点。

<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 {

}
?>

3 个答案:

答案 0 :(得分:2)

您可以在开始foreach迭代之前使用array_reverse()

$is_open = false;
$tpl['gallery_arr'] = array_reverse( $tpl['gallery_arr'], true );
foreach ($tpl['gallery_arr'] as $k => $v)

答案 1 :(得分:0)

在使用foreach迭代之前,您可以使用array_reverse简单地反转数组的顺序:

ImageIcon man;
ImageIcon grass;
public int xPosition=0;
public int yPosition=0;
public int oldX =0;
public int oldY = 0;

    class ButtonListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent evt) {
        oldX = xPosition;
        oldY = yPosition;    
        if(evt.getActionCommand() == Actions.east.name()){
            System.out.println("east!");
            if(xPosition<4){
                xPosition++;
            }
            else{
                System.out.println("can't go east!");
            }
        }
        if(evt.getActionCommand() == Actions.west.name()){
            System.out.println("west!");
            if(xPosition>0){
                xPosition--;
            }
            else{
                System.out.println("can't go west!");
            }
        }
        if(evt.getActionCommand() == Actions.north.name()){
            System.out.println("north!");
            if(yPosition>0){
                yPosition--;
            }
            else{
                System.out.println("can't go north!");
            }
        }
        if(evt.getActionCommand() == Actions.south.name()){
            System.out.println("south!");
            if(yPosition<4){
                yPosition++;
            }
            else{
                System.out.println("can't go south!");
            }
        }
        URL imageMan = getClass().getResource("man.png");
        man= new ImageIcon(imageMan);

        URL imageGrass = getClass().getResource("grass.jpg");
        grass= new ImageIcon(imageGrass);

        points[oldX][oldY].setIcon(grass);
        points[xPosition][yPosition].setIcon(man);

        System.out.println("codinates: ("+xPosition+","+yPosition+")");
    }
}

或者您可以使用计数器进行反向迭代(如果数组很大,可能会有更好的性能):

foreach ( array_reverse( $tpl['gallery_arr'] ) as $k => $v)

答案 2 :(得分:0)

所以看起来你可以使用array_reverse()函数

$tpl['gallery_arr'] = array_reverse( $tpl['gallery_arr'], true );
foreach ($tpl['gallery_arr'] as $k => $v){
...
}

或者使用带有反转参数的常规for循环。

for($k = count($tpl['gallery_arr']) - 1; $k >= 0; $k--){
$v = $tpl['gallery_arr'][$k];
...
}