Jump to content

Foreach for two arrays?


RynMan

Recommended Posts

Hi guys

 

Here's my code:

 

<?php 
foreach ($LNames as $val) {
  echo "it=s2.addItem(5,6,7,'$val',n,n,'','',n,n,n,n,n,112,20,2,0,0);\n";
}
?>

 

What I also want to do here, is have another variable adding values from another array.  Here's essentially what I want to do, but the code isn't correct.....

 

<?php 
foreach ($LNames as $val and $LinkNumber as $link) {
  echo "it=s2.addItem(5,6,7,'$val',n,n,'$link','$link',n,n,n,n,n,112,20,2,0,0);\n";
}
?>

 

In the earlier code, I have a loop assigning different values to an aray for $LNames.  At the same time, I'm adding values to the $LinkNumber array of the same integer.  So then, what I'm trying to do above, is have the following in my echo...

 

$LNames[0] along with $LinkNumber[0]

$LNames[1] along with $LinkNumber[1]

$LNames[2] along with $LinkNumber[2]

 

and so on......

 

Thanks for any help guys

Link to comment
Share on other sites

You can get the array index (key) for the array in a foreach by using the construct as key => value (see below). 

 

foreach ($LNames as $key => $val) {
    $link = $LinkNumber[$key];
    echo "it=s2.addItem(5,6,7,'$val',n,n,'$link','$link',n,n,n,n,n,112,20,2,0,0);\n";
}

 

Hope that helps. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.