473,386 Members | 1,702 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,386 software developers and data experts.

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($viscounter);
$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=".$v24h;
echo "<br>v30d=".$v30d;
echo "<br>v365d=".$v365d;
Jul 17 '05 #1
2 2198
> 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


I think I got it:

$avs = file($viscounter);
$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... :)
Jul 17 '05 #2
"deko" <no****@hotmail.com> wrote in message
news:yz*******************@newssvr27.news.prodigy. com...
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($viscounter);
$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=".$v24h;
echo "<br>v30d=".$v30d;
echo "<br>v365d=".$v365d;


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($viscounter);
$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=".$v24h;
echo "<br>v30d=".$v30d;
echo "<br>v365d=".$v365d;
Jul 17 '05 #3

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

Similar topics

6
by: ChronoFish | last post by:
Hi there, I want to iterate through an array starting at a known index. However the indexes are not linear. For example I have an array of events keyed by timestamp. $eventList = array...
1
by: | last post by:
I'm going a little crazy trying to learn how to use arrays as properties in VBScript classes. Hopefully someone can help. First, I can't figure out whether it's possible to iterate through the...
4
by: nicolas | last post by:
Hello,everybody I get a problem: 1. Have an Int Array; 2. Run iterate over a range of Array once; 3. Get Minimum result and No.2 minimum result;
5
by: | last post by:
Hello, I have an array like this: Array ( =Array ( =Array ( =7A585DC0
5
by: Martin Pöpping | last post by:
Hello, I want to iterate the second dimension of a 2-dim-array. Let´s say I have an array: double myArray and a given index: int i. Assume my index is given in want to iterate my array...
7
by: Jacob JKW | last post by:
I need to iterate over combinations of n array elements taken r at a time. Because the value of r may vary quite a bit between program invocations, I'd like to avoid simply hardcoding r loops. I...
5
by: Manish Tomar | last post by:
Hi, I know that using for in loop to iterate over array elements is a bad idea but I have a situation where it is tempting me to use it. I have a two dimensional sparse array say "arr" which...
1
by: nagarwal | last post by:
hi frnds, I am facing a prblm in displaying the contents of an ArrayList which contains 'String' Objects using logic:iterate tag.. example: in the Action : String s=null; String s1=null;...
25
RMWChaos
by: RMWChaos | last post by:
Any JSON experts out there? I'd like to know if it is possible, and if so how, to iterate through a JSON property list so that each iteration selects the next value for each object. Here is an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.