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