Build array from foreach loop?

For a Wordpress image gallery plugin, I need to take an array of integers (representing the ID of images), loop through the array and get certain pieces of data based on the integer/ID, and then build an array, which will then be JSON encoded.

For example, I have this piece of code that will spit out the images on the front end.

foreach($image_array as $key => $value) {
  $image = get_post_meta( $value, '_wp_attachment_metadata', true );    
  $full_src = wp_get_attachment_image_src($value, 'full')[0];
  $meta = wp_get_attachment_metadata($value);
  $large_file = $meta['sizes']['large']['file'];
  $thumb_src = wp_get_attachment_image_src($value, 'thumbnail')[0];
if ($gallery_style != "grid"){
	$items = array_slice($image_array,0,3);

  echo "<a data-lg-size=\"2500-2500\" class=\"gallery-item\" data-src=\"$full_src\" data-sub-html=\"Some static text\">
          <img class=\"img-fluid\" src=\"$thumb_src\" />
        </a>";
  }
}

Using the same $image_array, I need build a separate JSON array that looks like

[  {
      src:
        "imageurl.com",
      responsive:
        "imageurlresponsive.com",
      thumb:
        "imageurlthumb.com",
      subHtml: '<div><p>Some static text</p></div>`
    },
]

OK, what have you tried so far? It seems as if you have all the building blocks, and just need to create the array instead of (or as well as) outputting the information. I haven’t done much with JSON, is it just as simple as building your array and then calling json_encode() ?

(yes, it is.)

To build the structure he’s outlining, he will create a numeric array of associative arrays, and then json_encode the whole thing.

1 Like

Why not define a simple class instead of working with assoc arrays?

Is also a valid way of doing it. Most people will stick with an assoc. array. shrug

According to my experiences I would suggest you to create an empty array before the loop starts, And once you have done it add an associative array to it on each iteration. Try it!!!

Just having a tinker with a bit of PHP and possibly off by a country mile. Would something along these lines work?

// created a couple of user defined functions as I think it makes the code less jumbled
// and easier to reason with.

// possibly more arguments like $static_text needed
function create_image_link($full_src, $thumb_src) {
    // using heredoc syntax as I think it is more readable
    return <<<LINK
        <a 
            data-lg-size="2500-2500" 
            class="gallery-item" 
            data-src="$full_src" 
            data-sub-html="Some static text"
        >
            <img class="img-fluid" src="$thumb_src" />
        </a>
    LINK;
}

// don't know the format of the $img_props, but using the idea of a factory to create each object
function create_image_props($img_props) {
	list($img_url, $img_responsive, $img_thumb, $html) = $img_props
	
    return array(
      "src" => $img_url,
      "responsive" => $img_responsive,
      "thumb" => $img_thumb,
      "subHtml" => $html
    );
}

$images = [];

foreach($image_array as $key => $value) {

	$image = get_post_meta( $value, '_wp_attachment_metadata', true );    
	$full_src = wp_get_attachment_image_src($value, 'full')[0];
	$thumb_src = wp_get_attachment_image_src($value, 'thumbnail')[0];
	$meta = wp_get_attachment_metadata($value);
	$large_file = $meta['sizes']['large']['file'];

    if ($gallery_style != "grid"){
		$img_props = array_slice($image_array, 0, 3);
        $images.push(create_image_props($img_props));

		echo create_image_link($full_src, $thumb_src);
    }
}

$images_JSON = json_encode($images);

A bit of a shot in the dark. I am sure the experts can pull this apart :slight_smile:

Wouldnt this need to be an array of the four variables retrieved a few lines earlier, rather than a slice of the images?

I think you are right, that is off. I grabbed the OP’s original code.

$items = array_slice($image_array,0,3);

Where is subHtml, responsive coming from. Is src, $full_src and is thumb, $thumb_src? Really could do with seeing a sample of what $image_array looks like.

What are the variables $image, $meta and $large_file? They aren’t being used in the code, that I can see. What is their intended use. Are these used for the above props?

Maybe me being slow.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.