473,770 Members | 6,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;
Jul 17 '05 #1
2 2212
> 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($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... :)
Jul 17 '05 #2
"deko" <no****@hotmail .com> wrote in message
news:yz******** ***********@new ssvr27.news.pro digy.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($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;


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;
Jul 17 '05 #3

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

Similar topics

6
3957
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 (1064263264 => "event1", 10642635555 => "event2", 1064266666 => "event3", 1064267782 => "event4", 1064268812 => "event5"); I basically want to do a "for" or "foreach" but I don't necessarily want to start at key 1064263264 (event1).
1
6464
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 members of an array that I have assigned to a propety. Second, I'm finding the question of "Property Let" versus "Property Set" to be very confusing. I have this code that utilizes a custom class I've defined, called clsGroup: myString =...
4
6303
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
1706
by: | last post by:
Hello, I have an array like this: Array ( =Array ( =Array ( =7A585DC0
5
2700
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 with something like this:
7
7423
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 assume the best way to do this would be either using closures or creating some sort of iterator class. Any guidance on how to get started? Thanks,
5
2482
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 will have length to be some 50 but will have some elements like arr, arr, arr at random locations. Each of these elements is again an array (since it is two dimensional) say arr, arr & so on for arr also. Currently I've coded it like this: for...
1
2548
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; s="hi";
25
25450
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 example list: function myFunction() { createDOM({ 'id' : , 'dom' : , 'parent' : "content", 'form' : ,
0
9592
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10231
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.