SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Location
    England
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    skipping indexes in arrray loops

    Hi i want to loop through an setup array skipping an index at a time

    e.g. 1,2,3,4

    but looping 1,3,5,7 but the catch is when looping i want to display 2,4,6,8.

    for example

    $array = array("1", "top admin", "2", "middle admin", "3", "mod").

    foreach array = $value+2
    {
    echo "$value . "is number " . array(i+1);
    }

    sorry if this is quick but doing it on borrowed time. any ideas?

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,411
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    I guess you'll have to handle this yourself. Something like
    PHP Code:
    $array = array("1""top admin""2""middle admin""3""mod");

    $switch true;
    foreach (
    $array as $key=>$value) {
      
    $switch = !$switch;
      if (
    $switch) {
        echo 
    "$value is number $key<br />";
      }


  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could also use a for loop
    PHP Code:
    for ($i 0$m count($array); $i $m$i += 2) {
        echo 
    $i $array[$i 1];



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
  •