meltingpoint Posted June 25, 2009 Share Posted June 25, 2009 the file (db_file.txt)contains four things: monkey apple orange pear when I use $XX = file("db_file.txt", "r"); and then print_r($XX); it returns- Array([0]=>monkey[1]=>apple[2]=>orange[3]=>pear[4]=> ) using $size = count($XX); it gives - 5 So why is it giving a 5th element to the file? Link to comment https://forums.phpfreaks.com/topic/163649-count-function/ Share on other sites More sharing options...
Alex Posted June 25, 2009 Share Posted June 25, 2009 Because arrays start at indexing at 0, not 1. Say you have: $array = Array('number1', 'number2', 'number3'); $array[0] = number1 $array[1] = number2 $array[2] = number3 There's 3 elements, but the highest index is 2. Link to comment https://forums.phpfreaks.com/topic/163649-count-function/#findComment-863462 Share on other sites More sharing options...
meltingpoint Posted June 25, 2009 Author Share Posted June 25, 2009 Yes- but there are only 4 items being called in the file. The element [4]=> is empty. Therefore the count should be 4 not 5. ? Link to comment https://forums.phpfreaks.com/topic/163649-count-function/#findComment-863465 Share on other sites More sharing options...
MatthewJ Posted June 25, 2009 Share Posted June 25, 2009 Because there is an erroneous new line character after the last line... so it reads the blank line as well... that is why the 4th index is blank. Put the cursor on the line below the last item and backspace it up so the cursor is behind the last piece of data and it should correct it. Link to comment https://forums.phpfreaks.com/topic/163649-count-function/#findComment-863468 Share on other sites More sharing options...
meltingpoint Posted June 25, 2009 Author Share Posted June 25, 2009 \n - that explains the 5th element. I tried the cursor thing and it did not correct it. So I will have to figure out a way to trim the \n off the end in order to get a correct count. Any suggestion? Link to comment https://forums.phpfreaks.com/topic/163649-count-function/#findComment-863476 Share on other sites More sharing options...
MatthewJ Posted June 25, 2009 Share Posted June 25, 2009 Leave it there and subtract one from the count? Link to comment https://forums.phpfreaks.com/topic/163649-count-function/#findComment-863477 Share on other sites More sharing options...
meltingpoint Posted June 25, 2009 Author Share Posted June 25, 2009 ...............that sounds reasonable. (And easy- LOL). Thanks Link to comment https://forums.phpfreaks.com/topic/163649-count-function/#findComment-863484 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.