How to iterate through array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deko

    How to iterate through array?

    I have a file that contains the output of time(). A different time is on
    each line of the file, and each line represents a visit to the website. I
    want to calculate the total visits per day, month and year. So, I dump the
    file into an array, and then iterate through the array testing each element
    and incrementing a counter for each respective time period.

    But what is the best way to iterate through the array? The foreach($av)
    does not seem to be working. Any help is appreciated. Thanks.

    $viscounter = 'viscounter';
    // this is all that's in the file (will be hundreds of lines):
    1089828547
    1089828548
    1089828549
    1089828550

    $av = file($viscounte r);
    $v24h = 0;
    $v30d = 0;
    $v365d = 0;
    foreach($av) // ????????
    {
    if($av>time()-(3600*24))
    {
    ++$v24h;
    }
    if($av>time()-(3600*24*30))
    {
    ++$v30d;
    }
    if($av>time()-(3600*24*365))
    {
    ++$v365d;
    }
    }
    echo "<br>v24h=".$v2 4h;
    echo "<br>v30d=".$v3 0d;
    echo "<br>v365d=".$v 365d;


  • deko

    #2
    Re: How to iterate through array?

    > I have a file that contains the output of time(). A different time is on[color=blue]
    > each line of the file, and each line represents a visit to the website. I
    > want to calculate the total visits per day, month and year. So, I dump[/color]
    the[color=blue]
    > file into an array, and then iterate through the array testing each[/color]
    element[color=blue]
    > and incrementing a counter for each respective time period.
    >
    > But what is the best way to iterate through the array? The foreach($av)
    > does not seem to be working. Any help is appreciated. Thanks.
    >
    > $viscounter = 'viscounter';
    > // this is all that's in the file (will be hundreds of lines):
    > 1089828547
    > 1089828548
    > 1089828549
    > 1089828550[/color]

    I think I got it:

    $avs = file($viscounte r);
    $v24h = 0;
    $v30d = 0;
    $v365d = 0;
    foreach ($avs as $val)
    {
    if($val>(time()-(3600*24)))
    {
    ++$v24h;
    }
    if($val>(time()-(3600*24*30)))
    {
    ++$v30d;
    }
    if($val>(time()-(3600*24*365)))
    {
    ++$v365d;
    }
    }
    echo "<br>v24h = ".$v24h;
    echo "<br>v30d = ".$v30d;
    echo "<br>v365d = ".$v365d;

    seems to be working... :)


    Comment

    • kingofkolt

      #3
      Re: How to iterate through array?

      "deko" <nospam@hotmail .com> wrote in message
      news:yzkJc.2042 5$uE2.18848@new ssvr27.news.pro digy.com...[color=blue]
      > I have a file that contains the output of time(). A different time is on
      > each line of the file, and each line represents a visit to the website. I
      > want to calculate the total visits per day, month and year. So, I dump[/color]
      the[color=blue]
      > file into an array, and then iterate through the array testing each[/color]
      element[color=blue]
      > and incrementing a counter for each respective time period.
      >
      > But what is the best way to iterate through the array? The foreach($av)
      > does not seem to be working. Any help is appreciated. Thanks.
      >
      > $viscounter = 'viscounter';
      > // this is all that's in the file (will be hundreds of lines):
      > 1089828547
      > 1089828548
      > 1089828549
      > 1089828550
      >
      > $av = file($viscounte r);
      > $v24h = 0;
      > $v30d = 0;
      > $v365d = 0;
      > foreach($av) // ????????
      > {
      > if($av>time()-(3600*24))
      > {
      > ++$v24h;
      > }
      > if($av>time()-(3600*24*30))
      > {
      > ++$v30d;
      > }
      > if($av>time()-(3600*24*365))
      > {
      > ++$v365d;
      > }
      > }
      > echo "<br>v24h=".$v2 4h;
      > echo "<br>v30d=".$v3 0d;
      > echo "<br>v365d=".$v 365d;
      >
      >[/color]

      You've used foreach() incorrectly. You must give the foreach() function a
      variable to fill with each item in the $av array. So to correct your code:

      $viscounter = 'viscounter';
      $av = file($viscounte r);
      $v24h = 0;
      $v30d = 0;
      $v365d = 0;
      foreach($av as $visit) // ????????
      {
      if($visit>time( )-(3600*24))
      {
      ++$v24h;
      }
      if($visit>time( )-(3600*24*30))
      {
      ++$v30d;
      }
      if($visit>time( )-(3600*24*365))
      {
      ++$v365d;
      }
      }
      echo "<br>v24h=".$v2 4h;
      echo "<br>v30d=".$v3 0d;
      echo "<br>v365d=".$v 365d;


      Comment

      Working...