Which array function?

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

    Which array function?

    I have a file containing only UNIX timestamps that I pull into an array with
    file($myarray)

    1157950336
    1157950334
    1157949470
    1157948606
    1157947742
    1157946878
    1157946014

    I need to get a count of timestamps that fall between specific times - those
    that are less than 24-hours old, those that are between 24-hours old and 30-days
    old, and so forth.

    I thought about using array_filter and a function like this:

    function getlast24h($vis its24h)
    {
    return ($visits24h TIMEAGO24H);
    }

    $last24h_array = array_filter($m yarray, "getlast24h ");

    where TIMEAGO24H is a constant representing a timestamp of time() - 24hours

    but then I cannot get any additional time segments (such as last 30days, last 3
    months, etc) - since $myarray will have had those timestamps filtered out.

    I need to start with the smallest section (24-hours) rather than the largest,
    otherwise I could filter downward.

    Is there a function that will do this? Any examples would be helpful...

    Thanks in advance.

  • Tim Martin

    #2
    Re: Which array function?

    deko wrote:
    I have a file containing only UNIX timestamps that I pull into an array
    with file($myarray)
    >
    I need to get a count of timestamps that fall between specific times -
    those that are less than 24-hours old, those that are between 24-hours
    old and 30-days old, and so forth.
    >
    I thought about using array_filter and a function like this:
    >
    function getlast24h($vis its24h)
    {
    return ($visits24h TIMEAGO24H);
    }
    >
    $last24h_array = array_filter($m yarray, "getlast24h ");
    >
    where TIMEAGO24H is a constant representing a timestamp of time() - 24hours
    >
    but then I cannot get any additional time segments (such as last 30days,
    last 3 months, etc) - since $myarray will have had those timestamps
    filtered out.
    array_filter() doesn't modify its argument, since it is passed by value
    not reference. Therefore $myarray won't have the timestamps removed, and
    can be used again.

    Tim

    Comment

    • frothpoker

      #3
      Re: Which array function?

      Pass the array to a function

      set a variable = to now - 24hrs.

      Loop through the array and either build a new array of the values or
      add one to a counter if the value falls in the required range

      return either the counter or the new array.

      This way you will not destroy the original array.

      Obiron

      Tim Martin wrote:
      deko wrote:
      I have a file containing only UNIX timestamps that I pull into an array
      with file($myarray)

      I need to get a count of timestamps that fall between specific times -
      those that are less than 24-hours old, those that are between 24-hours
      old and 30-days old, and so forth.

      I thought about using array_filter and a function like this:

      function getlast24h($vis its24h)
      {
      return ($visits24h TIMEAGO24H);
      }

      $last24h_array = array_filter($m yarray, "getlast24h ");

      where TIMEAGO24H is a constant representing a timestamp of time() - 24hours

      but then I cannot get any additional time segments (such as last 30days,
      last 3 months, etc) - since $myarray will have had those timestamps
      filtered out.
      >
      array_filter() doesn't modify its argument, since it is passed by value
      not reference. Therefore $myarray won't have the timestamps removed, and
      can be used again.
      >
      Tim

      Comment

      • deko

        #4
        Re: Which array function?

        array_filter() doesn't modify its argument, since it is passed by value not
        reference. Therefore $myarray won't have the timestamps removed, and can be
        used again.
        Thanks, that helped. Seems like these functions want constants. I was had
        trouble with a variable.

        Here's what I ended up with:

        function getbaz_current( $var)
        {
        return ($var <= MAXDAILY);
        }

        function getbaz_new($var )
        {
        return ($var < TIMEAGO30D && $var >= MAXDAILY);
        }

        $baz_current = array_filter($c ounter_array, "getbaz_current ");
        $baz_new = array_filter($c ounter_array, "getbaz_new ");


        Comment

        Working...