Looping through Array and pulling out specific pairs

I am creating a slider, and I need to pull out specific values from an array to add them to the slider.

From what I understand, the best way to do this is a foreach loop. I found a function to spit out the images as an array, but I can’t figure out how to pull out the specific values.

I’ve started with $image_array = spit_it_image_array();

The markup for the slider would look like:

<div id="post-default-slider" class="royalSlider rsDefault">
   <div class="aSlide">
      <img src="from-the-array.com/img1.jpg">
      <p> maybe the caption goes here </p>

   <div class="aSlide">
      <img src="from-the-array.com/img2.jpg">
      <p> maybe the caption goes here </p>

   <div class="aSlide">
      <img src="from-the-array.com/img3.jpg">
      <p> maybe the caption goes here </p>


An example of an array for an image looks like:

array(6) { [0]=> object(stdClass)#2538 (14) { ["ID"]=> string(3) "320" ["post_id"]=> string(3) "372" ["date_gmt"]=> string(19) "2021-04-26 13:49:29" ["user_id"]=> string(1) "1" ["other_id"]=> string(1) "0" ["title"]=> string(0) "" ["caption"]=> string(0) "" ["file"]=> string(28) "/2021/04/restaurants19-2.jpg" ["mime_type"]=> string(10) "image/jpeg" ["menu_order"]=> string(1) "0" ["featured"]=> string(1) "1" ["is_approved"]=> string(1) "1" ["metadata"]=> string(2371) "a:5:{s:5:"width";i:580;s:6:"height";i:386;s:4:"file";s:27:"2021/04/restaurants19-2.jpg";s:5:"sizes";a:14:{s:9:"thumbnail";a:4:{s:4:"file";s:27:"restaurants19-2-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:27:"restaurants19-2-300x200.jpg";s:5:"width";i:300;s:6:"height";i:200;s:9:"mime-type";s:10:"image/jpeg";}s:10:"td_218x150";a:4:{s:4:"file";s:27:"restaurants19-2-218x150.jpg";s:5:"width";i:218;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:10:"td_324x400";a:4:{s:4:"file";s:27:"restaurants19-2-324x386.jpg";s:5:"width";i:324;s:6:"height";i:386;s:9:"mime-type";s:10:"image/jpeg";}s:10:"td_485x360";a:4:{s:4:"file";s:27:"restaurants19-2-485x360.jpg";s:5:"width";i:485;s:6:"height";i:360;s:9:"mime-type";s:10:"image/jpeg";}s:8:"td_80x60";a:4:{s:4:"file";s:25:"restaurants19-2-80x60.jpg";s:5:"width";i:80;s:6:"height";i:60;s:9:"mime-type";s:10:"image/jpeg";}s:9:"td_100x70";a:4:{s:4:"file";s:26:"restaurants19-2-100x70.jpg";s:5:"width";i:100;s:6:"height";i:70;s:9:"mime-type";s:10:"image/jpeg";}s:10:"td_265x198";a:4:{s:4:"file";s:27:"restaurants19-2-265x198.jpg";s:5:"width";i:265;s:6:"height";i:198;s:9:"mime-type";s:10:"image/jpeg";}s:10:"td_324x160";a:4:{s:4:"file";s:27:"restaurants19-2-324x160.jpg";s:5:"width";i:324;s:6:"height";i:160;s:9:"mime-type";s:10:"image/jpeg";}s:10:"td_324x235";a:4:{s:4:"file";s:27:"restaurants19-2-324x235.jpg";s:5:"width";i:324;s:6:"height";i:235;s:9:"mime-type";s:10:"image/jpeg";}s:10:"td_356x220";a:4:{s:4:"file";s:27:"restaurants19-2-356x220.jpg";s:5:"width";i:356;s:6:"height";i:220;s:9:"mime-type";s:10:"image/jpeg";}s:10:"td_356x364";a:4:{s:4:"file";s:27:"restaurants19-2-356x364.jpg";s:5:"width";i:356;s:6:"height";i:364;s:9:"mime-type";s:10:"image/jpeg";}s:10:"td_533x261";a:4:{s:4:"file";s:27:"restaurants19-2-533x261.jpg";s:5:"width";i:533;s:6:"height";i:261;s:9:"mime-type";s:10:"image/jpeg";}s:10:"td_534x462";a:4:{s:4:"file";s:27:"restaurants19-2-534x386.jpg";s:5:"width";i:534;s:6:"height";i:386;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"0";s:8:"keywords";a:0:{}}}" ["type"]=> string(11) "post_images" }

so what have you tried with a foreach?

So far this is what I have… It returns when I try and echo $images, but it does spit out the array when I var_dump($images)… Not sure what I am doing wrong…

$post_id = get_the_ID();
$post_images = geodir_get_images( $post_id );
		$images = array();
		if ( ! empty( $post_images ) ) {
			foreach ( $post_images as $image ) {
				$row = array();
				$row['id'] = $image->ID;
				$row['title'] = stripslashes_deep( $image->title );
				$row['src'] = geodir_get_image_src( $image, 'original' );
				$row['thumbnail'] = geodir_get_image_src( $image, 'thumbnail' );
				$row['featured'] = (bool) $image->featured;
				$row['position'] = $image->menu_order;

				$images[] = $row;
		echo $images;

Ah okay.
You can’t echo an array. An Array is a complex object, and echo just wants a string. If you toString an array, you just get the string “Array”.
print_r will output an array’s structure for you if you’re just looking to visually inspect the contents.(You might want to wrap it in a <pre> tag to make it easier to read, or else View Source.)

Dang… Okay, that makes sense. That won’t work the way I thought it would, then.

Ultimately, I need the markup as such

<div class="slider">
///loop through all of the images here.
  <div class="slide">

I’ve managed to do something similar before, but in that case, I had already had a function that spit out an array with everything I needed.

Well let me be clear.

You can’t echo an array.
You can echo strings inside an array.

foreach ( $image in $images) { echo $image['id']; }
is perfectly valid, as long as the thing that’s in “id” is a string (or string-able).

Okay that makes more sense. Arrays are more a “mapping” of data and as such can’t be echo’d like strings can.

What if I just defined some variables within the foreach (since I need functions to do get the data). Is that a legitimae practice?

foreach ($image_row as $image){
$original_url = geodir_get_image_src( $image, 'original' );
echo "<img src=\"$orginal_url\">

Sure. Just be careful, because the variables arent cleaned up at the end - a foreach isnt a new variable scope.

At the end of that code block (once you properly close that string on the third line, anyway), $original_url will have whatever the last value was.