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(); ?>

I would like to push the images array into a list of arrays accessible by a key. $imagesArray = array($images); doesn't append the $images array, only overwrites it.

1
  • $imagesArray[] = $images; Commented May 24, 2017 at 4:20

3 Answers 3

1

your overwriting continuously instead of pushing into array

Method 1 :

$imagesArray[] = $images;

Method 2 :

use array_push function like this

array_push($imagesArray, $images);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use array_push() function like

 array_push($imagesArray, $images);

Learn more about the Function here

Comments

0

You could use array_merge!

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

This would append the new array to the end of the old one.

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

The following quote from the source seems note-worthy :

"If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored."

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.