Results 1 to 9 of 9

Thread: Loop thru an array

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Loop thru an array

    Is there a way to loop thru an array?
    $split = explode(":",$contents)

    How do i do somethign which each split[x]?

  2. #2
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Loop thru an array

    yes of course.

  3. #3
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Loop thru an array

    let me go check my handy dandy exercises.

    here it is... sorry just placed everything here when I need to test something I read at dox.


    PHP Code:

    <?php

    /*

    spent the last year
    rocky mountain way
    couldnt get much higher
    out to pasture
    think its safe to say
    time to open fire
    */


    $arr = array(   => array( => "spent"=> "the"=> "last"=> "year"),
                    
    => array( => "rocky"=> "mountain"=> "way"),
                    
    => array( => "couldnt"=> "get"=> "much"=> "higher"),
                    
    => array( => "out"=> "to"=> "pasture"),
                    
    => array( => "think"=> "its"=> "safe"=> "to"5=> "say"),
                    
    => array( => "time"=> "to"=> "open"=> "fire")
                );



    ?>
    <html>
    <body>
        <table>
            <?PHP
                
    /*$reccnt = 0;
                for($cnt1 = 1; $cnt1 <= count($arr[1]); $cnt1++ ){
                    for($cnt2 = 1; $cnt2 <= count($arr[2]); $cnt2++){
                        for($cnt3 = 1; $cnt3 <= count($arr[3]); $cnt3++){
                          for($cnt4 = 1; $cnt4 <= count($arr[4]); $cnt4++){
                            for($cnt5 = 1; $cnt5 <= count($arr[5]); $cnt5++){
                              for($cnt6 = 1; $cnt6 <= count($arr[6]); $cnt6++){
                                echo "<TR>";
                                echo "<TD>";
                                echo ++$reccount;
                                echo "</TD>";
                                echo "<TD>";
                                echo $arr[1][$cnt1];
                                echo "</TD>";
                                echo "<TD>";
                                echo $arr[2][$cnt2];
                                echo "</TD>";
                                echo "<TD>";
                                echo $arr[3][$cnt3];
                                echo "</TD>";
                                echo "<TD>";
                                echo $arr[4][$cnt4];
                                echo "</TD>";
                                echo "<TD>";
                                echo $arr[5][$cnt5];
                                echo "</TD>";
                                echo "<TD>";
                                echo $arr[6][$cnt6];
                                echo "</TD>";
                                echo "</TR>";
                              }
                            }
                          }
                        }
                    }
                } */
            
    ?>
        </table>
        <?PHP
        
    print "<BR>";
        print 
    "TESTING";
        
        
    $detail array_fill(02"value");
        
        foreach( 
    $detail as $det) {
            print 
    "<BR>".$det;
        }
        
        
        print 
    "/**********************************************************************/"."<BR>";
        
    $mystring "Iamthestring";
        print 
    $mystring."<BR>";
        print 
    substr($mystring01).substr($mystringstrlen($mystring) - 6).'=Istring';
        
        print 
    '<BR>';
        
    $month "12";
        print 
    date("Y/m/d"mktime(0,0,0$month 1132005)) . ' - ' date("Y/m/d"mktime(0,0,0$month132020));
        
        print 
    '<BR>';
        
    $strtoSplit "1,2,3,4,5,data";
        
    $arraydata split(","$strtoSplit );
        
        
    var_dump$arraydata);
        
        
        
    ?>
    </body>
    </html>

  4. #4
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Loop thru an array

    check out the foreach part


    you could do like
    PHP Code:
    $aString "1, 2, 3, 4, 5";

    $myArr split(","$aString);

    foreach(
    $myArr as $x ){
         print 
    $x;

    or just use the for loop like I did above.
    Last edited by oceanebelle; Jul 21st, 2005 at 07:55 PM. Reason: ehehe.. split function was not right. :D

  5. #5
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Loop thru an array

    oooh.. I learned one function today... explode()... hmm
    and the difference.

    explode is much faster because no evaluation overhead for regular expressions..
    split is used if you'd like to use regular expressions.

    I should read the dox better next time.

  6. #6

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Loop thru an array

    How do i tell which iteration of the loop i am on?

  7. #7
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Loop thru an array

    Quote Originally Posted by |2eM!x
    How do i tell which iteration of the loop i am on?
    well you could just use the for loop.

    if you really want to know where you are on... I use the for loop more often .. check out the commented code. i was practising on two dimensional arrays.. populated manually.

  8. #8

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Loop thru an array

    after i posted i thought about ways to do it

    i could simply set a variable and add one x++ or use the for loop


  9. #9
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Loop thru an array

    yeah like the for loop in my commented sample code.

Posting Permissions

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



Click Here to Expand Forum to Full Width