Php 将数组推入数组 “class=”我的自定义类“alt=”画廊图像“/>

Php 将数组推入数组 “class=”我的自定义类“alt=”画廊图像“/>,php,arrays,Php,Arrays,我想将images数组放入一个可以通过键访问的数组列表中。$imagesArray=array($images);不追加$images数组,只覆盖它。您不断地覆盖,而不是推入数组 方法1: <?php $images =[]; $imagesArrays = []; //The Loop $loop = new WP_Query( array( 'post_type' => 'gallery', 'posts_per_page' => 100 )

我想将images数组放入一个可以通过键访问的数组列表中。$imagesArray=array($images);不追加$images数组,只覆盖它。

您不断地覆盖,而不是推入数组

方法1:

<?php 

$images =[];
$imagesArrays = [];
//The Loop
$loop = new WP_Query( array( 'post_type' => 'gallery', 
        'posts_per_page' => 100 ) 
            ); 
while ( $loop->have_posts() ) : $loop->the_post(); 

    if ( get_post_gallery() ) :
            $gallery = get_post_gallery( get_the_ID(), false );

            /* Loop through all the image and output them one by one */
            foreach( $gallery['src'] as $src ) : ?>
                <img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />
                <?php
            //Creates an Array Gallery Images from the Post
            //Array ( 
            //        [0] => http://velnikolic.com/gallery/wp-content/uploads/2017/04/file4741298583098-1-1024x653.jpg 
            //        [1] => http://velnikolic.com/gallery/wp-content/uploads/2017/04/file9221293737060-1024x683.jpg 
            //        [2] => http://velnikolic.com/gallery/wp-content/uploads/2017/04/file4741298583098-1024x653.jpg 
            //       )    
            $images = $gallery['src'];
            endforeach;



    endif;
    //Push $images arrays into one array
    $imagesArray = array($images);
    print_r($imagesArray);

endwhile; wp_reset_query(); ?>
方法2:

<?php 

$images =[];
$imagesArrays = [];
//The Loop
$loop = new WP_Query( array( 'post_type' => 'gallery', 
        'posts_per_page' => 100 ) 
            ); 
while ( $loop->have_posts() ) : $loop->the_post(); 

    if ( get_post_gallery() ) :
            $gallery = get_post_gallery( get_the_ID(), false );

            /* Loop through all the image and output them one by one */
            foreach( $gallery['src'] as $src ) : ?>
                <img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />
                <?php
            //Creates an Array Gallery Images from the Post
            //Array ( 
            //        [0] => http://velnikolic.com/gallery/wp-content/uploads/2017/04/file4741298583098-1-1024x653.jpg 
            //        [1] => http://velnikolic.com/gallery/wp-content/uploads/2017/04/file9221293737060-1024x683.jpg 
            //        [2] => http://velnikolic.com/gallery/wp-content/uploads/2017/04/file4741298583098-1024x653.jpg 
            //       )    
            $images = $gallery['src'];
            endforeach;



    endif;
    //Push $images arrays into one array
    $imagesArray = array($images);
    print_r($imagesArray);

endwhile; wp_reset_query(); ?>
像这样使用array_push函数

$imagesArray[] = $images;

您可以使用
array\u push()
函数,如

array_push($imagesArray, $images);

了解有关函数的更多信息

您可以使用array\u merge

$imagesArray=array\u merge($imagesArray,$images)

这将把新数组附加到旧数组的末尾

资料来源:

以下来源的引述似乎值得注意:

如果两个数组中都存在数组键,则 将使用第一个数组,并从
第二个数组将被忽略。“

$imagesArray[]=$images;