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
https://forums.phpfreaks.com/topic/197350-foreach-for-two-arrays/
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. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.