Breaking out of a ForEach?

I am working from the code below. I need to create a script that creates an array from the stuff that is getting echo’d out at the end.

I think I am on the right track here.

The problem is I don’t know how to break out of the foreach pool to add the and some other stuff for the script. If I add it within the foreach, it will just spit it out over and over, obviously…

What’s the right way to do this?

$rows = get_field('gallery_repeater');
if( $rows ) {
    foreach( $rows as $row ) {
        $repeater_image = $row['repeater_image'];
            $repeater_image_url = $repeater_image['url'];
            $repeater_image_width_full = $repeater_image['width'];
            $repeater_image_height_full = $repeater_image['height'];
            $repeater_caption = $row['gallery_repeater_caption'];
            $repeater_title = $row['gallery_repeater_title'];
            $repeater_dl_url = $row['download_image'];
        if ($repeater_caption) {
            $image_caption = $repeater_caption;
        else {
            $image_caption = $repeater_image['caption'];
        if ($repeater_dl_url){
            $dl_url = $repeater_dl_url;
        else {
            $dl_url = $repeater_image_url;
        if ($repeater_title){
          $image_title = $repeater_title;
        else {
          $image_title = $repeater_image ['title'];
          ///// I need to break out and add:
          var pswpElement = document.querySelectorAll('.pswp')[0]; 
          ////End break --- echo this stuff to build an array:
     echo "{ src: '$repeater_image_url', w: $repeater_image_width_full,h: $repeater_image_height_full,msrc: '$repeater_image_url',  title: '$image_caption' },";
        ///break out again and add:
        // define options (if needed)
var options = {
    // optionName: 'option value'
    // for example:
    index: 0 // start at first slide



You might be looking for break which when used in a foreach loop, ends the processing of the foreach

Where you have placed this code, your ‘break out’ (which could just as easily be part of your echo) will execute on every loop.

If you’re simply trying to echo some stuff out to the HTML, that’s what echo is for. If you absolutely must break the PHP code, you close the PHP tag (?>) and then reopen it when you want to resume PHP processing (<?php)

If you want to terminate the foreach loop early, you need a condition to break out on. What triggers the break?

I may have misunderstood what you are trying to do. I think you are trying to build a javascript array from inside a php foreach loop? If so, you can use the js push() function to add one value each iteration of the foreach loop thus:

<!doctype html>
    var jsCars =[];
      //create php array
      $cars = array (
        foreach($cars as $value){
          echo '
            jsCars.push(" '.$value.' ");


I think your array may be two dimensional which would make it a bit more involved but I think the basic principle would be the same.

Apologies if I have misunderstood.

Did you get it sorted?