tags:

views:

884

answers:

10

I'm writing a php script where I call

$lines = file('base_list.txt');

to break a file up into an array. The file has over 100,000 lines in it, which should be 100,000 elements in the array, but when I run

print_r($lines);
exit;

the array only contains 7280 elements.

So I'm curious, WTF? Is there a limit on the amount of keys an array can have? I'm running this locally on a dual-core 2.0Ghz with 2GB of RAM (Vista & IIS though); so I'm a little confused how a 4MB file could throw results like this.

Edit: I have probably should have mentioned that I had previously set memory_limit to 512MB in php.ini as well.

+1  A: 

I believe it is based on the amount of available memory as set in the php.ini file.

Cory
Wouldn't you expect an exception or something if a file exceeded that memory limit?
Greg Hewgill
Not really. The interpreter would clean up whatever it could, and halt any additional consumption; beyond that, it might log the occurrence.
The Wicked Flea
+2  A: 

Edit: As Rob said, if your application is running out of memory, chances are it won't even get to the print_r line. That's kinda my hunch as well, but if it is a memory issue, the following might help.

Take a look in the php.ini file for this line, and perhaps increase it to 16 or more.

memory_limit = 8M

If you don't have access to php.ini (for example if this was happening on a shared server) you can fix it with a .htaccess file like this

php_value memory_limit 16M

Apparently some hosts don't allow you to do this though.

nickf
A: 

I'm gonna agree with Cory. I'm thinking your PHP is probably configured default memory of 8MB, which 4MB x 2 is already more. The reason for the x2 is because you have to load the file, then to create the array you need to have the file in memory again. I'm just guessing, but that would make sense.

Are you sure PHP isn't logging an error?

Darryl Hein
+2  A: 

Is it possible there is an inherent limit on the output from print_r. I'd suggest looking for the first and last line in the file to see if they are in the array. If you were hitting a memory limit inserting into the array you would never have gotten to the print_r line.

Rob Walker
+2  A: 

Two suggestions:

1) Count the actual number of items in the array and see whether or not the array is the correct number of entries (therefore eliminating or identifying print_r() as the culprit)

2) Verify the input...any chance that the line endings in the file are causing a problem? For example, is there a mix of different types of line endings? See the manual page for file() and the note about the auto_detect_line_endings setting as well, though it's unlikely this is related to mac line endings.

Jay
+1  A: 

every time ive run out of memory in PHP i've received an error message stating that fact. So, I'd also say that if you're running out of memory then the script wouldn't get to the print_r()

try enabling auto_detect_line_endings in php.ini or by using
ini_set('auto_detect_line_endings', 1). There may be some line endings that windows doesn't understand & this ini option could help. more info about this ini option can be found here

arin sarkissian
+1  A: 

You should use count to count the number of items in an array, not print_r. What if output of this large array was aborted because of timeouts or something else? Or some bug/feature in print_r?

Christian Davén
+3  A: 

Darryl Hein,

Yeah, there isn't anything in the error logs. I even increased error reporting and still nothing relevant to print_r().

In response to Jay: I ran

echo count($lines);

and I get a result of 105,546 but still print_r() only displays 7280.

Taking Rob Walker's advice I looped over all the elements in the array and it actually contained all the results. This leads me to believe the issue is with print_r() itself instead of a limit to array size.

Another weird thing is that I tried it on one of my REHL servers and the result was as it should be. Now I don't want to blame this on Windows/IIS but there you go.

With the above in mind I think this question should be re-titled as it's no longer relevant to arrays but print_r.

Eric Lamb
this is rather useful to know.. maybe you could put this as a community owned answer so that you can accept it.
paan
On http://au.php.net/print_r there is several replacements for print_r which should work correctly (assuming it's a limitation of print_r)
dbr
A: 

If you're outputting to something like Internet Explorer, you might want to make sure it can display all the information you're trying to put there. I know there's a limit to an html page, but I'm not sure what it is.

A: 

PHP's print_r function does have limitations. However, even though you don't "see" the entire array printed, it is all there. I've struggled with this same issue when printing large data objects.

It makes debugging difficult, if you must see the entire array you could create a loop to print every line.

foreach ($FileLines as $Line) echo $Line;

That should let you see all the lines without limitation.

sirlancelot