problem with count()-ing array members

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DrPL
Forum Commoner
Posts: 26
Joined: Wed Oct 07, 2009 4:22 pm

problem with count()-ing array members

Post 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
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: problem with count()-ing array members

Post 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.
DrPL
Forum Commoner
Posts: 26
Joined: Wed Oct 07, 2009 4:22 pm

Re: problem with count()-ing array members

Post 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
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: problem with count()-ing array members

Post by Mark Baker »

$count = count(explode( "::::", $file_contents )) - 1;
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: problem with count()-ing array members

Post by JNettles »

I suggest not affixing that extra :::: to the end of your file. Instead of posting "DATA::::" post "::::DATA".
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: problem with count()-ing array members

Post by JNettles »

Using regular expression functions in this case is overkill - a simple change to how the data is stored will fix the problem.
DrPL
Forum Commoner
Posts: 26
Joined: Wed Oct 07, 2009 4:22 pm

Re: problem with count()-ing array members

Post 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
Post Reply