随机播放并仅在PHP foreach循环中显示某个项目

时间:2017-01-06 09:50:58

标签: php arrays

我正试图在for each循环中进行随机播放,只有19个项目。我使用了shuffle()if (++$i == 19) {break;},但问题是一次返回12项,一次是13次,另一次是14.我做错了什么?

以下是要检查的代码:

<?php
    $i = 0;
    shuffle($children);
    foreach ($children as $child) {
        if ($child['name_total'] > 0) {
            ?>
            <li>
            <?php echo ($child['filter_id'] == 2 ? "<span class='Verified'><i class='fa fa-check-circle'></i></span>" : ""); ?>
                <div class="CatImg"><a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><img alt="<?php echo $child['name']; ?>" src="<?php echo $child['thumb']; ?>"/></a></div>
                <a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?><span class="Total"><?php echo $child['name_total']; ?></span></a>
            </li>
        <?php
        }
        if (++$i == 19) {
            break;
        }
    }
?>

4 个答案:

答案 0 :(得分:2)

仅在打印结果时增加$ i。在开始新的foreach循环之前检查值。

 <?php
$i = 0;
shuffle($children);
foreach ($children as $child) {
    if ($child['name_total'] > 0) {
        ?>
        <li>
        <?php echo ($child['filter_id'] == 2 ? "<span class='Verified'><i class='fa fa-check-circle'></i></span>" : ""); ?>
            <div class="CatImg"><a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><img alt="<?php echo $child['name']; ?>" src="<?php echo $child['thumb']; ?>"/></a></div>
            <a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?><span class="Total"><?php echo $child['name_total']; ?></span></a>
        </li>
    <?php
     $i++;
    }
    if ($i == 19) {
        break;
    }
}
?>

答案 1 :(得分:2)

我认为你想要改组数组,

使用此功能,

function shuffle_assoc(&$array) {
    $keys = array_keys($array);

    shuffle($keys);

    foreach ($keys as $key) {
        $new[$key] = $array[$key];
    }

    $array = $new;

    return true;
}
$i = 0;
shuffle_assoc($children);

foreach ($children as $child) {
    if ($child['name_total'] > 0) {
        ?>
        <li>
        <?php echo ($child['filter_id'] == 2 ? "<span class='Verified'><i class='fa fa-check-circle'></i></span>" : ""); ?>
            <div class="CatImg"><a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><img alt="<?php echo $child['name']; ?>" src="<?php echo $child['thumb']; ?>"/></a></div>
            <a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?><span class="Total"><?php echo $child['name_total']; ?></span></a>
        </li>
    <?php
    }
    if (++$i == 19) {
        break;
    }
}
?>

我希望这可以帮助您解决问题。

答案 2 :(得分:1)

代码的简化版

foreach ($children as $child) {

    if ($child['name_total'] > 0) {
        // display stuff
    }

    if (++$i == 19) {
        break;
    }
}

所以即使没有显示任何内容,$i每次都会递增。如果$child['name_total']为0,则不会显示任何内容 - 但$i仍然会增加。

如果您想要显示19个项目,则只需在实际显示内容时增加$i

foreach ($children as $child) {

    if ($child['name_total'] > 0) {
        // display stuff

        if (++$i == 19) {
            break;
        }
    }
}

答案 3 :(得分:-1)

请尝试以下操作。

numSplits