View Full Version : Foreach with multiple arrays?
jacksont123
02-26-2007, 10:41 PM
$href = array('1.html', '2.gif', '3.php', '4.jpg');
$name = array('My html file', 'My gif file', 'My php file', 'My jpeg file');
How do I make it so that I can combine both arrays to make a link?
I want the output to be:
<a href="1.html">My html file</a>
<a href="2.gif">My gif file</a>
<a href="3.php">My php file</a>
<a href="4.jpg">My jpg file</a>
Foreach only works with one array
//this does not work
foreach($file as $f && $name as $n) {echo '<a href=\'$f'\'>'$n.'</a><br>';}
Is there a way for foreach to work with multiple arrays or is there an alternative method?
I'm using links as an example, just so you know.
djr33
02-26-2007, 11:05 PM
I don't know of any way to use foreach like that, specifically, though I don't know all that much about it.
Here's an option:
for ($n=0; isset($ar1[$n]) && isset($ar2[$n]; $n++) {
echo $ar1[$n].$ar2[$n];
}
Modify as you like.
Aside from this, I'd suggest doing--
$n=0;
foreach ($ar1 as $thing) {
$bigar[$n][1] = $thing;
$n++;
}
$n=0;
foreach ($ar2 as $thing) {
$bigar[$n][2] = $thing;
$n++;
}
foreach ($bigar as $part) {
echo $part[1].$part[2];
}
Just some ideas.
I don't promise the scripts will work without some alterations/specifications.
jacksont123
02-26-2007, 11:09 PM
How would I use this in relation to my first post?
Can you please make a post with the arrays on the first post and please explain how to do it?
jacksont123
02-26-2007, 11:13 PM
Another way to do this would be to combine the arrays...
Can you please answer this question too?
http://www.dynamicdrive.com/forums/showthread.php?t=17961
djr33
02-26-2007, 11:19 PM
Here is the commented version of the two scripts above:
In both, assume that $ar1 is the first array, and $ar2 is the second array.
echo $ar1[$n].$ar2[$n]; means echo the value of that part of the first array, then that part of the second array. You could place them into links as you wish.
for ($n=0; isset($ar1[$n]) && isset($ar2[$n]; $n++) { //for loop:
//set $n to zero first time
//while both the $ar1 and $ar2 at $n are set, like $ar2[7], etc.
echo $ar1[$n].$ar2[$n];
}
$n=0; //start counting at 0, like array counters do.
foreach ($ar1 as $thing) { //for each value of $ar1
$bigar[$n][1] = $thing; //set $bigarray at $n at 1 to that part
$n++; //add one to $n, continue
}
$n=0; //repeat for array 2
foreach ($ar2 as $thing) {
$bigar[$n][2] = $thing; //now use the 2 value of the array
$n++;
}
foreach ($bigar as $part) { //go through each chunk of the array
echo $part[1].$part[2]; //now, like above, that part has [1], and [2]
}
Basically, this one combines both arrays into a master, bigger array, then runs through a foreach loop of that, then echoes the inner bits of that array.
Note: I also fixed a typo above. I put $ar1/$ar2 in the foreach loop, but I meant $thing.
djr33
02-26-2007, 11:21 PM
I was writing the above post when you posted.
please only use one thread for the problem... keeps things simple.
The above example DOES combine the arrays, in what I see as a logical way. You should able to do the other method if needed, using some of the code above, though I'm not sure if that would work too well. You're setting the NAME of each part to a string, so calling that later would be tough.
The other (easier) option is to use the each (http://www.php.net/each)() function with a while loop.
djr33
02-27-2007, 12:13 AM
Ah. K. Haven't tried that myself. I figured there might be a 'right' answer for this specifically.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.