Javascript 如何从一个子foreach获取值,并将其放入父循环中,然后在数组中洗牌所有值

Javascript 如何从一个子foreach获取值,并将其放入父循环中,然后在数组中洗牌所有值,javascript,php,html,arrays,Javascript,Php,Html,Arrays,我正在使用WordPress和ACF Gallery。在每一篇文章中,我都有一个图片库 我需要从所有图库中获取所有图像,并对它们进行洗牌 我怎样才能做到这一点 现在我有这个: <?php if ( $query->have_posts() ) : ?> <?php while ( $query->have_posts() ) : $query->the_post(); $images = get_field( '

我正在使用WordPress和ACF Gallery。在每一篇文章中,我都有一个图片库

我需要从所有图库中获取所有图像,并对它们进行洗牌

我怎样才能做到这一点

现在我有这个:

    <?php if ( $query->have_posts() ) : ?>
        <?php while ( $query->have_posts() ) : $query->the_post();
            $images = get_field( 'gallery' );
            $size = 'full';
            if ( $images ) : ?>
                <?php foreach( $images as $image ): ?>
                    <img class="post-gallery" src="<?php echo $image['url']; ?>">
                <?php endforeach; ?>
           <?php endif; ?>
           <?php if (has_post_thumbnail()) : ?>
                <div class="post" style="background-image: url(<?php the_post_thumbnail_url(); ?>);"></div>
<?php endif; ?>

">

我个人会让PHP完成繁重的工作,正如你所说,创建一个洗牌数组,JS可以循环使用。例如:

<?php
    $collectedImages = [];
    if ($query->have_posts()) {
        while($query->have_posts()) {
            $query->the_post();
            $images = get_field('gallery');
            $size = 'full';
            if ($images === false) {
                continue;
            }

            foreach ($images as $image) {
                $collectedImages[] = $image['url'];
            }
        }
    }
    shuffle($collectedImages);

    foreach ($collectedImages as $image) {
        // Render it! 
    }
?>

更多信息:


祝大家今天愉快!如果有javascript部分,请添加。@NinaScholz Hi!问题已更新
<?php
    $collectedImages = [];
    if ($query->have_posts()) {
        while($query->have_posts()) {
            $query->the_post();
            $images = get_field('gallery');
            $size = 'full';
            if ($images === false) {
                continue;
            }

            foreach ($images as $image) {
                $collectedImages[] = $image['url'];
            }
        }
    }
    shuffle($collectedImages);

    foreach ($collectedImages as $image) {
        // Render it! 
    }
?>