SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    London, UK
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Multiple foreach in array?

    Is it possible to add foreach together as similar to my attempt below?

    PHP Code:
    foreach ($cars[type] as $info)
    foreach (
    $bikes[engines] as $speed => $output)
    {
    echo 
    "<tr><td>$info</td><td>$speed</td><td>$output</td></tr>";
    }
    echo 
    "</table>"

    It is working but my array is repeated * 3 the output in echo.

    I have tried playing with it such as
    PHP Code:
    foreach (($cars[type] as $info) && ($bikes[engines] as $speed => $output))
    {
    echo 
    "<tr><td>$info</td><td>$speed</td><td>$output</td></tr>";
    }
    echo 
    "</table>"
    But i could not get it to work.

    Can you see anything wrong with the syntax?
    Add your web design / hosting companies to my directory - Web Hosting Directory
    Want to join our panel of experts? - Hosting Forums ( top right of forums -expert drop down box for more info ).

  2. #2
    SitePoint Enthusiast monkey56657's Avatar
    Join Date
    Jun 2007
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can u provide a short snippet with what the output shud be like...Im not exactly sure what you are aiming to do

  3. #3
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    London, UK
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by monkey56657 View Post
    Can u provide a short snippet with what the output shud be like...Im not exactly sure what you are aiming to do

    This is for a php project i am doing for a course - therefore i can't use someone elses code - aganst the rules.

    Having said that, i have been here for hours trying to work this out - so if you can pass me onto any websites which may help (tutorials) or point me in the right direction i would appreciated it.

    The code i created is below,

    PHP Code:
    <?php
    $terminator_bid 
    ="10";
    $terminator "$terminator_bid";
    $alien "3";
    $batman "22";

    $ads = array ( 
    "ad_positions" => array ( "Terminator" => "$terminator""Alien" => "$alien""Batman" => "$batman"),
    "positions" => array ( "1""2""3" )
    );
    /* asort - assending by values (numbers, ect)*/
    arsort($ads[ad_positions]);
    /* arsort - desecnding by values (numbers, ect)*/
    echo "<table border='1' cellpadding='7'>";
    echo 
    "<tr><th>Position</th><th>DVD Name</th><th>Bid Price</th></tr>";
    foreach (
    $ads[positions] as $rank
    foreach (
    $ads[ad_positions] as $list => $price)
    {
    echo 
    "<tr><td>$rank</td><td>$list</td><td>$price</td></tr>";
    }
    echo 
    "</table>";
    ?>
    At the moment everything repeats 3 times - i guess the array is being looped or something.

    It should only contain the first 3 positions.

    Positions 1, 2, 3 with DVD name as Batman, Alien & Terminator plus 3 seperate bid prices.

    As you may see it repeats...
    Add your web design / hosting companies to my directory - Web Hosting Directory
    Want to join our panel of experts? - Hosting Forums ( top right of forums -expert drop down box for more info ).

  4. #4
    SitePoint Enthusiast monkey56657's Avatar
    Join Date
    Jun 2007
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THis is helpful.

    First of all may i advise you that lines 2 to 10 can be simplified a lot. But i wont explain how, its the rules

    However i believe that you do not really need to use 2 foreach loops.

    Your $ads[positions] array has no 'named' addresses, ie they are identified by the order they were set starting from 0. We will keep one of the foreach loops you have coded as below.

    PHP Code:
    foreach($ads['ad_positions'] as $list => $price){


    To start going through each of the elements from the other array we can use a counter variable, which increments by one each time.

    Therefore we add in

    PHP Code:
    $Counter 0;

    foreach(
    $ads['ad_positions'] as $list => $price){

        .....do 
    sumthing here......
        
    $Counter $Counter 1;


    Okay now we have a way of going through each of the other elements as the foreach loops goes thru the other array. Its now just a case of doing the output.

    PHP Code:
    $Counter 0;

    foreach(
    $ads['ad_positions'] as $list => $price){

        echo 
    "<tr><td>".$ads['positions'][$Counter]."</td><td>".$list."</td><td>".$price."</td></tr>";
        
    $Counter $Counter 1;


    The value from each of the $ads[positions] elements is outputted as long as the number of elements in both 'subarrays' are the same.

    Do not confuse the value of $Counter with the values in your array. Even though the $Counter is set to start at 0, it will fetch the value of "1" from array index 0.

    Hopefully now i have explained the code thoughrally you understand perfectly so cant get marked as cheating hehe

  5. #5
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    London, UK
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Monkey...

    I had to make sure your help was ok - so i showed my College Tutor George & he was ok with it as it was only a variable difference.

    Anyhow - i ended doing the array a completely different way in the end, so it did not matter.

    Thanks,

    - Dave
    Add your web design / hosting companies to my directory - Web Hosting Directory
    Want to join our panel of experts? - Hosting Forums ( top right of forums -expert drop down box for more info ).

  6. #6
    SitePoint Zealot glowdot's Avatar
    Join Date
    Jun 2007
    Location
    Santa Monica, CA
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even though you did it another way, this might still be useful to someone.

    The problem with your code is that you appear to not understand the benefit of nesting for loops. When the loops are nested, you are basically adding dimensions to your search, so in the case of foreach, one would expect your arrays to be related. As you coded, they aren't.

    Here's a simple example:

    Code:
    foreach($students AS $student_name) {
        foreach($courses[$student_name] AS $course) {
            echo "$student_name is taking $course.\n";
        }
    }
    Basically, $students is an array of student names, which is then used in the inner loop as indices for an array of arrays. The first loop goes through all the student names to use as indices on the inner loop to get an array of course, which is then foreached to get the individual course. In your code snippet, your arrays appear to have no relation to each other in terms of index.

    If they are related to each other by numerical index, then you should be using a for loop like so:

    Code:
    for ($i = 0; $ < count($array1); $i++) {
        echo $array1[$i]."\n";
        echo $array2[$i]."\n";
    }
    Cross fingers and pray that you stay in-bounds.
    Advertise on Glowfoto
    banners as low as $25/month, text ads $10/month
    Share 10 million impressions per month!


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
  •