473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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($visits24h)
{
return ($visits24h TIMEAGO24H);
}

$last24h_array = array_filter($myarray, "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.

Sep 11 '06 #1
3 1061
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($visits24h)
{
return ($visits24h TIMEAGO24H);
}

$last24h_array = array_filter($myarray, "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
Sep 11 '06 #2
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($visits24h)
{
return ($visits24h TIMEAGO24H);
}

$last24h_array = array_filter($myarray, "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
Sep 11 '06 #3
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($counter_array, "getbaz_current");
$baz_new = array_filter($counter_array, "getbaz_new");
Sep 11 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: simon | last post by:
Always when I need data reader in my programs, I simply have functions, which creates it for me: Dim rdr As SqlDataReader dim sql as string sql="myStoredProcedure" rdr =...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
52
by: Darklight | last post by:
Question: Write a function named sumarrays() that accepts two arrays that are the same size. The function should add each element in the array together and place the values in the thrid array. ...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
4
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person...
7
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h>...
3
by: d d | last post by:
I have a large object with many sub-objects. They may go down 4 or 5 levels and I'm not sure how best to code a method that does operations on that data. Here's an attempt to make it an isolated...
8
by: jodleren | last post by:
Hi! I have a function, a part of my code which I can use as a function. It will return 2 arrays, and I am wondering what way to do so. Both arrays hold strings, there are no special keys. 1)...
12
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.