SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot charles_i's Avatar
    Join Date
    Apr 2007
    Location
    Toronto, Canada
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I loop through an indexed PHP array?

    This should be pretty simple but all the searches I do turn up looping through an associative array. I've created an array in a Session contining links clicked so that when someone returns from a thank you page the links they clicked on can be disabled. When you go from the page with links to the thank you page I add it to the Session array:

    //add $linkVisted array to the session
    $_SESSION['linkVisted'] = $linkVisted;

    //add the next number to the array
    array_push($linkVisted,$link);

    This works and returns the following output when I return to the links page:

    Array ( [linkVisted] => Array ( [0] => 3 [1] => 3 [2] => 1 [3] => 1 [4] => 8 [5] => 9 ) )

    Now, how do I loop through this array to compare each link's number with these numbers? When I try this I get an array length of 1 - because there is only one array - so how do get the number of elements (9 here) and loop through them?

    for ($i = 1; $i < count($linkVisted); ++$i) {
    echo $linkVisted[$i];
    }

    Thanks!

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How did you get this?
    Code:
    Array ( [linkVisted] => Array ( [0] => 3 [1] => 3 [2] => 1 [3] => 1 [4] => 8 [5] => 9 ) )
    Was it the output of
    PHP Code:
    print_r($_SESSION); 
    ?

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach($linkVisted as $thisLink) {
       echo 
    $thisLink;

    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,139
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Code:
    if(in_array($currentLink,$linksVisited)) {
      /* current link has been previously visited */
    }

  5. #5
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's hard to see exactly what you did, but this may work:

    PHP Code:
    $total count($linkVisted['linkVisted']);
    for (
    $i 0$i $total$i++) {
        echo 
    $linkVisted[$i];

    It's returning 1 item because you have an array inside of an array. The first (and only item) of the outer array is the inner array. I'm not sure what you're doing but you can either access it directly (see above), or loop through if it you have multiple inner arrays:

    PHP Code:
    foreach ($linkVisted as $iLink) {
        
    $total count($iLink);
        for (
    $i 0$i $total$i++) {
            echo 
    $iLink[$i];
        }

    Oh, and always put computations outside the for loop (in this case, count()) because if you place it in the condition then it has to count the array for every loop, where since you know the value is going to stay constant over the scope of the loop it is faster if you place it on the outside:

    PHP Code:
    for ($i 0$i count($array); $i++) {
        
    // BAD

    PHP Code:
    $total count($array);
    for (
    $i 0$i $total$i++) {
        
    // GOOD


  6. #6
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your print out is saying that there are two arrays. Below i illustrated a litted
    function to loop through if there are more than one array, otherwise print the
    results.

    //assuming these are the values in your session
    PHP Code:
    $_SESSION['linkVisited']= Array ( "linkVisted" => Array ( => ,=> ,=> ,=> ,=> ,=> ) );

    link_visited($_SESSION['linkVisted']);
        function 
    link_visited($sess){
            foreach(
    $sess as $kk=>$vv){
                if(
    is_array($vv)){ link_visited($vv); }else{ echo $vv."<br>";}
            }
        } 

  7. #7
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One last thing just place a global variable in your foreach loop where each result is echoed out to count the number of results. Good luck.

  8. #8
    SitePoint Zealot charles_i's Avatar
    Join Date
    Apr 2007
    Location
    Toronto, Canada
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the code hints. Let me explain what I'm trying to accomplish. I have a page with a set of links (seminars attended) - when you click on the links you go to a 'thank you' page with a ratings form. When you click 'submit' it takes you back to the first page of links. I want to disable the links that have already been chosen during the PHP Session.

    So I have given each link a 'link' number that is sent through the URL to the 'thank you' page. I pick it up here
    Code:
    $linkVisted = $_GET['link'];
    and then add it to a Session array
    Code:
    $_SESSION['linkVisted'] = $linkVisted;
    Now I'm in trouble - how do I push the new link onto the array each time? Everything that I've tried as the first argument in 'array_push' returns the error 'First argument should be an array'. What is the actual name of the Session array? $_SESSION['linkVisted'] doesn't work, neither does $linkVisted.

    When I return to the links page I want to write a foreach loop to check which numbers are in the array. Then I can check each link with a function call to see if the link number is in it. If it is I can disable the link.

    Thanks
    Charles

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $_SESSION['linkVisted'][] = $_GET['link'];
    print_r($_SESSION['linkVisted']); 
    http://www.php.net/manual/en/language.types.array.php

  10. #10
    SitePoint Zealot charles_i's Avatar
    Join Date
    Apr 2007
    Location
    Toronto, Canada
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks crmalibu - I will check this out tomorrow - just leaving work. To answer your original question - 'how did you get this?' 'Was it the output of print?' yes it was (when I got it to work) I was showing that that's what I wanted in the array.


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
  •