473,471 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Foreach problem

I have the following issue:

I read lines of information from a log file. Each line is identified by an
ID number.
Due to nature of the logging process it is possible blocks are logged more
than once, but in these cases the IDs will be similar.
In my PHP script I temporarily store all read ID numbers in an array. When
done reading I want to sort the array and then loop through it to check for
doubles. Right now all I am interested in is knowing how many doubles there
are.

The simple log lines look like this:

33,222,671 string
33,222,814 string
etc.
part of my code:

$idarr = array ();
while (!feof ($logfile))
{
$line = $gets ($logfile,1024);
$items = explode (" ",$line);
$idarr[] = $items[0]; // add the id to an array ( I guess as a
string...)
// rest of code, irrelevant to issue
}
$idarr = sort ($idarr); // make sure doubles are in consecutive order
$tempid = "";
$doubles = 0;
foreach ($idarr as $id)
{
if ($id == $tempid) // evals to true when this id is same as the one
before this
{
$doubles++; // which means we have found another double
}
$tempid = $id;
}
echo $doubles;

The code runs until it hits the foreach, then it returns a warning:
Warning: Invalid argument supplied for foreach() in
/srv/www/htdocs/area51/script.php on line 315
The $doubles value echoed to the screen is 0

Can anyone point out where I am wrong here ? I know the code isn't exactly
rocket science, but hey, we all have to start somewhere!

TIA!
Pjotr
Jul 17 '05 #1
2 2269
Pjotr,

I would check the array with count($idarr) because the error yu are getting
could be the result of an empty array in which case you will need to look at
the wasy you are parsing the file.

Peace,

jg

"For example John Smith" <zp*******@xz4all.nl> wrote in message
news:40***********************@news.sunsite.dk...
I have the following issue:

I read lines of information from a log file. Each line is identified by an
ID number.
Due to nature of the logging process it is possible blocks are logged more
than once, but in these cases the IDs will be similar.
In my PHP script I temporarily store all read ID numbers in an array. When
done reading I want to sort the array and then loop through it to check for doubles. Right now all I am interested in is knowing how many doubles there are.

The simple log lines look like this:

33,222,671 string
33,222,814 string
etc.
part of my code:

$idarr = array ();
while (!feof ($logfile))
{
$line = $gets ($logfile,1024);
$items = explode (" ",$line);
$idarr[] = $items[0]; // add the id to an array ( I guess as a
string...)
// rest of code, irrelevant to issue
}
$idarr = sort ($idarr); // make sure doubles are in consecutive order
$tempid = "";
$doubles = 0;
foreach ($idarr as $id)
{
if ($id == $tempid) // evals to true when this id is same as the one
before this
{
$doubles++; // which means we have found another double
}
$tempid = $id;
}
echo $doubles;

The code runs until it hits the foreach, then it returns a warning:
Warning: Invalid argument supplied for foreach() in
/srv/www/htdocs/area51/script.php on line 315
The $doubles value echoed to the screen is 0

Can anyone point out where I am wrong here ? I know the code isn't exactly
rocket science, but hey, we all have to start somewhere!

TIA!
Pjotr

Jul 17 '05 #2
jerrygarciuh wrote:
Pjotr,

I would check the array with count($idarr) because the error yu are
getting could be the result of an empty array in which case you will
need to look at the wasy you are parsing the file.

Peace,

jg

Thanks Jerry! The problem was the sort (). Sort does not return an array but
true or false (1, 0)
So I changed:

$idarr = sort ($idarr);
to
sort ($idarr);

That did it! Your tip to echo count ($idarr) was a great help!
It runs smoothly now!
Thanks again!
Pjotr
Jul 17 '05 #3

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

Similar topics

0
by: Randell D. | last post by:
Folks, Ever since reading an interesting article in Linux Format on PHP whereby suggested code writing was made that could enhance performance on a server, I've started testing various bits of...
7
by: Phil | last post by:
Hi, I read somewhere that the new version (v1.1) has improved the performance of 'foreach' over 'for'. Is that true? I did some measurements and I still think for has an upperhand... ? Phil
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
13
by: cody | last post by:
foreach does implicitly cast every object in the collection to the specified taget type without warning. Without generics this behaviour had the advantage of less typing for us since casting was...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
9
by: news.microsoft.com | last post by:
I am looping through an iteration and I would like to test the next item but if its not the one that I want how do I put it back so that when my foreach continues it is in the next iteration? ...
7
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and...
9
tlhintoq
by: tlhintoq | last post by:
Situation: A Windows Form with a single User Control containing 4 custom controls. This lets me drop the User Control on its own form to make it a dialog, or make it part of a larger configuration...
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
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
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,...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.