0

I currently have:

foreach($Opts['img'] as $img)
{
}

I want to add:

foreach($Opts['lnk'] as $lnk)

Which I initially read, to do so just put them as follows:

foreach($Opts['img'] as $img)
{
    foreach($Opts['lnk'] as $lnk)
    {
    }
}

However this duplicates all of the results so I end up with 9 images instead of 3.

Is there a way of getting the info from both array's in the same query? I want to end up with $img showing the image address and $lnk showing the link address.

4
  • no. foreach loops on a single array only. if you want to loop on two arrays in parallel, you'll have to merge the arrays into a single one and loop that, or have some kind of key-mapping system to relate the two arrays' entries to each other. Commented Oct 27, 2015 at 16:17
  • Are the keys the same in each array? Commented Oct 27, 2015 at 16:23
  • @AbraCadaver Yes they are Commented Oct 27, 2015 at 16:24
  • whats your array look like? Commented Oct 27, 2015 at 16:27

1 Answer 1

1

For identical keys, just use the key in the foreach:

foreach($Opts['img'] as $key => $img)
{
    $lnk = $Opts['lnk'][$key];

    echo "$img and $lnk";
}
Sign up to request clarification or add additional context in comments.

Comments

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.