RynMan Posted April 2, 2010 Share Posted April 2, 2010 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 More sharing options...
salathe Posted April 2, 2010 Share Posted April 2, 2010 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 https://forums.phpfreaks.com/topic/197350-foreach-for-two-arrays/#findComment-1035858 Share on other sites More sharing options...
ignace Posted April 2, 2010 Share Posted April 2, 2010 If both arrays have different key indexes, you could alternatively use: while (next($array1) && next($array2)) { Link to comment https://forums.phpfreaks.com/topic/197350-foreach-for-two-arrays/#findComment-1035867 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.