0

I am creating a basic foreach loop gallery function, but this gallery function I am trying to make is a little different because I am merging all the gallery images into one.

And there are multiple gallery arrays that I need to combine into one, however these gallery arrays are contained in one big array, and I'm struggling to figure out how to combine them because they are nested in the main array.

See how my array is structured below...

$gallerys = get_field( 'gallery_images' );
var_dump($gallerys);

-

array(2) {
  [0]=>
  array(2) {
    ["gallery_name"]=>
    string(11) "Gallery One"
    ["gallery_images"]=>
    array(1) {
      [0]=>
      array(10) {
        ["id"]=>
        int(373)

        ...etc etc

      }
    }
  }
  [1]=>
  array(2) {
    ["gallery_name"]=>
    string(11) "Gallery Two"
    ["gallery_images"]=>
    array(1) {
      [0]=>
      array(10) {
        ["id"]=>
        int(542)

        ...etc etc

      }
    }
  }
}


Can anyone point me in the right direction with updating the $gallerys variable with a new array which combines all ["gallery_images"] data into one.

So the idea is I can do this...

if( $gallerys ):

foreach( $gallerys as $gallery ):

echo $gallery['id'].'<br/>';

endforeach;

endif;

And it will output this...

373
542


Thanks in advance for any help you can provide.

Josh

3 Answers 3

2

This should work

$images=array();

foreach( $gallerys as $gallery ){

   $images=array_merge($images, $gallery[ ["gallery_images"]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Are you copy-pasting my answer? ;)
1

This should work

$images = array();
foreach($gallerys as $gallery) {
   $images = array_merge($images, $gallery['gallery_images']);
}

2 Comments

Perfect this worked a beaut! +1 - Noticed you and charlietfl both answered simultaneously - how do I pick a winner?
Hover your mouse over the "... mins ago", I was 30 sec earlier ;) Hence my comment on his answer.
1
$all_gallery_images = array();

foreach($gallerys as $gallery) {
   $all_gallery_images[] = $gallery["image"];
}

1 Comment

Not sure if this works? I know applied this method, fixed the typos, returns null. Thanks anyway solved it above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.