Jump to content

[Solved] 2 arrays inside 1 foreach


Fran
 Share

Recommended Posts

Hi, I'm using this piece of code to retrieve the names of two arrays of images and it works really well. But my question is how can I use only one foreach so i can use both arrays inside of it? Thanks very much.

$precioschico = $page->get("planos");
$preciosgrande = $page->get("mapas");

foreach( $preciosgrande as $preciogrande ) {
		echo $preciogrande . '<br/>';
		}

foreach( $precioschico as $preciochico ) {
		echo $preciochico . '<br/>';
		}

 

Link to comment
Share on other sites

Sorry, was on mobile so couldn't add a code sample. 

$precioschico = $page->get("planos");
$preciosgrande = $page->get("mapas");
$precios = $precioschico->makeCopy(); // So we don't change the variable itself on the next line
$precios->import($preciosgrande);

foreach( $precios as $precio ) {
		echo $precio . '<br/>';
}

 

  • Like 4
Link to comment
Share on other sites

Another approach using WireArray::and() which implicitly creates a copy of the Wire(or Page)Array:

$precioschico = $page->get("planos");
$preciosgrande = $page->get("mapas");
$precios = $precioschico->and($preciosgrande);
// Or all in one step: $precios = $page->get("planos")->and($page->get("mapas"));

foreach( $precios as $precio ) {
		echo $precio . '<br/>';
}

 

  • Like 4
Link to comment
Share on other sites

Thanks LostKobrakai. You're being really helpful. This is the first time I have both arrays on the same foreach. I'm intending to use it so I can click on an image from one array and it opens its corresponding bigger(and slightly different) from the other one. I really don't know if I'm going into the right direction with this.

I'd like to achieve something like this on the foreach:

echo "<div class='col-md-6 col-lg-4'>";
echo "<a href='{$preciosgrandeimg->url}' class='thumbnail' rel='prettyPhoto[galeriaplanos2]'>
<img class='img-responsive' src='{$precioschicoimg->url}' />
</a>";
echo "</div>";

Maybe I'm a little more clear with this.

Thanks very much for your help.

Link to comment
Share on other sites

5 hours ago, BitPoet said:

Another approach using WireArray::and() which implicitly creates a copy of the Wire(or Page)Array:


$precioschico = $page->get("planos");
$preciosgrande = $page->get("mapas");
$precios = $precioschico->and($preciosgrande);
// Or all in one step: $precios = $page->get("planos")->and($page->get("mapas"));

foreach( $precios as $precio ) {
		echo $precio . '<br/>';
}

 

Thanks Bit Poet for your help... maybe I'm not trying to combine them into one array but rather work with both inside the foreach statement. Please take a look at my response to LostKobrakai above.

Thanks very much again.

Link to comment
Share on other sites

I see, that's of course different, and - since there's no direct relationship between two image fields - doable but dangerous in case one of the images is missing, and keeping then in sync isn't easy once the list gets long.

$precioschico = $page->get("planos");
$preciosgrande = $page->get("mapas");

for($i = 0; $i < $precioschico->count(); $i++) {
  $chicoimg = $precioschico->eq($i);
  $grandeimg = $preciosgrande->eq($i);
  echo "
    <div class='col-md-6 col-lg-4'>
      <a href='{$grandeimg->url}' class='thumbnail' rel='prettyPhoto[galeriaplanos2]'>
        <img class='img-responsive' src='{$chicoimg->url}' />
      </a>
    </div>
  ";
}

To make the code a little simpler and avoid errors by images not matching up, you might consider creating a repeater field with two image fields (one named "grande" and one "chico", each set to "Single item"). Then you could run a simple loop over your repeater field.

foreach($page->repeaterimages as $img) {
  echo "
    <div class='col-md-6 col-lg-4'>
      <a href='{$img->grande->url}' class='thumbnail' rel='prettyPhoto[galeriaplanos2]'>
        <img class='img-responsive' src='{$img->chico->url}' />
      </a>
    </div>
  ";
}

If you set the column width for both image fields to 50%, you can show and edit them side by side in the repeater list on the page. The page editor would look like this:

TwoImageRepeater.png

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...