Advanced Search

Results 1 to 8 of 8

Thread: Foreach with multiple arrays?

  1. #1
    Join Date
    Jan 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Foreach with multiple arrays?

    PHP Code:
    $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:
    PHP Code:
    <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

    PHP Code:
    //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.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    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:

    PHP Code:
    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--

    PHP Code:
    $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.
    Last edited by djr33; 02-26-2007 at 10:15 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jan 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

  4. #4
    Join Date
    Jan 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Another way to do this would be to combine the arrays...

    Can you please answer this question too?
    http://www.dynamicdrive.com/forums/s...ad.php?t=17961

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    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.

    PHP Code:
    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];

    PHP Code:
    $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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The other (easier) option is to use the each() function with a while loop.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    Ah. K. Haven't tried that myself. I figured there might be a 'right' answer for this specifically.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •