0

我有以下 foreach 循环:

<?php $arrayforward = get_field('background_slider');
        $arrayreversed = array_reverse($arrayforward);
        $count=1;

        foreach($arrayreversed as $subarray) {

        $subfield1 = $subarray['background_image'];
        $subfield2 = $subarray['background_image_alt'];
        $subfield3 = $subarray['text_image'];
        $subfield4 = $subarray['text_image_alt']; ?>

        <div id="slide<?php echo $count; ?>" class="contentslider_wrapper">
            <div class="fullpageimage">
                <img id="slideImg<?php echo $count; ?>" src="<?php echo $subfield1; ?>" alt="<?php the_sub_field('background_image_alt'); ?>" />
                <img id="textImg<?php echo $count; ?>" class="slidetext" src="<?php echo $subfield2; ?>" alt="<?php the_sub_field('text_image_alt'); ?>" />
            </div>
        </div>

        <?php $count++; }; ?>

我需要帮助让计数反转。我正在寻找的输出如下:

<div id="slide4"></div>
<div id="slide3"></div>
<div id="slide2"></div>
<div id="slide1"></div>

对此的任何帮助都会很棒。

4

3 回答 3

1

更改$count=1;$count = sizeof($arrayreversed);

$count++;_$count--;

于 2012-12-17T17:44:01.183 回答
0

使用数组的大小作为初始计数然后每次迭代减去 1 怎么样?

<?php $arrayforward = get_field('background_slider');
    $arrayreversed = array_reverse($arrayforward);
    $count=count($arrayreversed);

    foreach($arrayreversed as $subarray) {

    $subfield1 = $subarray['background_image'];
    $subfield2 = $subarray['background_image_alt'];
    $subfield3 = $subarray['text_image'];
    $subfield4 = $subarray['text_image_alt']; ?>

    <div id="slide<?php echo $count; ?>" class="contentslider_wrapper">
        <div class="fullpageimage">
            <img id="slideImg<?php echo $count; ?>" src="<?php echo $subfield1; ?>" alt="<?php the_sub_field('background_image_alt'); ?>" />
            <img id="textImg<?php echo $count; ?>" class="slidetext" src="<?php echo $subfield2; ?>" alt="<?php the_sub_field('text_image_alt'); ?>" />
        </div>
    </div>

    <?php $count--; }; ?>
于 2012-12-17T17:45:31.447 回答
0

首先获取数组中键的数量并将其分配为$count,如下所示:

$count = count($arrayreversed);

然后替换为$count++$count--一切就绪。

于 2012-12-17T17:43:52.240 回答