Page 1 of 1

problem with count()-ing array members

Posted: Wed Oct 07, 2009 4:46 pm
by DrPL
Hi,
This is my first time here, please be gentle! :wink:

I have a file that is a series of data, delimetered by :::: in reverse order. The file is updated (prepended, if thats a word!) with new data so it reads something like
October09::::October07::::September30:::: etc etc.

I got the following code from the php.net website and it works fine, as far as I can see:

Code: Select all

 
$ini_handle2 = fopen("masterfile.txt", "r");
$ini_contents2 = fread($ini_handle2, filesize("masterfile.txt"));
fclose($ini_handle2);
 
$handle2 = fopen("masterfile.txt", "w+");
    $writestring2 = $dayfile . "::::" . $ini_contents2; // write the new data ahead of the old
 
if (fwrite($handle2, $writestring2) === false) 
{
    echo "Cannot write to text file. <br />";          
}
       fclose($handle2);
 
 
But, in another php file, I have the following:

Code: Select all

 
$f = "masterfile.txt"; 
$file_contents = file_get_contents( $f );
$arr = explode( "::::", $file_contents ); // get the individual data, and place in an array 
 
    foreach( $arr as $i )
    {    // etc. etc.
 
 
But when I do a count($arr), I get one more data entry than is present. For instance,
if the masterfile contains just "October09::::" count returns 2 !

Can anyone see what is amiss?

Thanks

Paul

Re: problem with count()-ing array members

Posted: Wed Oct 07, 2009 5:03 pm
by JNettles
When explode sees the "October09::::" at the end of your script it is probably dumping an empty string into the last field of your array. It see's that last :::: and assumes that you want the data before it and after it, which in this case is an empty string. I'm guessing your text file looks like this..


October09::::October07::::September30::::

Where you end on a ::::, so it explode creates the extra entry to your array. Do var_dump($arr) and I'll bet you'll see the last entry of your array is an empty string.

Re: problem with count()-ing array members

Posted: Thu Oct 08, 2009 6:00 am
by DrPL
Thanks, it looks like that is the problem. I'm wondering why this is though?

As for a solution, a horrible way of doing it would be to count the array elements in the foreach statement and then, when the last one comes along, ditch it. Can anyone suggest a better solution?

Many thanks!

Paul

Re: problem with count()-ing array members

Posted: Thu Oct 08, 2009 6:06 am
by Mark Baker
$count = count(explode( "::::", $file_contents )) - 1;

Re: problem with count()-ing array members

Posted: Thu Oct 08, 2009 9:15 am
by JNettles
I suggest not affixing that extra :::: to the end of your file. Instead of posting "DATA::::" post "::::DATA".

Re: problem with count()-ing array members

Posted: Thu Oct 08, 2009 3:38 pm
by JNettles
Using regular expression functions in this case is overkill - a simple change to how the data is stored will fix the problem.

Re: problem with count()-ing array members

Posted: Thu Oct 08, 2009 3:58 pm
by DrPL
Many thanks, I've decided to just dump the last element in the list. I can see why php did what it did but it was maddening!

:wink:

Best wishes

Paul