2017-05-24 169 views
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(); ?> 

我想將圖像數組推入可通過鍵訪問的數組列表中。 $ imagesArray = array($ images);不附加$ images數組,只覆蓋它。將陣列推入陣列

+0

$ imagesArray [] = $ images; – JYoThI

回答

1

您的連續覆蓋,而不是推到數組

方法1:

$imagesArray[] = $images; 

方法2:

使用array_push函數這樣

array_push($imagesArray, $images); 
0

可以使用array_push()功能像

array_push($imagesArray, $images); 

瞭解更多關於功能here

0

你可以使用array_merge!

$ imagesArray = array_merge($ imagesArray,$ images);

這會將新數組追加到舊數組的末尾。

來源:http://php.net/manual/en/function.array-merge.php

從源下面引用似乎值得關注的:

「如果數組鍵在兩個數組存在,那麼從 第一個數組元素將被用於和匹配鍵 第二個數組中的元素將被忽略。「