Re: Multidimensional arrays, Memory exhausted

From: Date: Mon, 08 Oct 2001 22:28:55 +0000
Subject: Re: Multidimensional arrays, Memory exhausted
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-70393@lists.php.net to get a copy of this message
There is a configuration directive which specifies how much memory could be allocated by PHP (per request I think). http://www.php.net/manual/en/configuration.php#memory-limit ----- Original Message ----- From: "* R&zE:" <renze@datalink.nl> To: "PHP General Mailinglist" <php-general@lists.php.net> Sent: Monday, October 08, 2001 12:36 PM Subject: [PHP] Multidimensional arrays, Memory exhausted > Hi there, > > Can anyone tell me why arrays cause such a problem in PHP? > > I've got a tab separated file that I want to process. When I read > it in memory PHP gives me an error that the allowed memory size is > exhausted: > > Allowed memory size of <size> bytes exhausted .... > > The construction I use is that I open the file and use fgetcsv to > read the "records". This returns a 1D array for each record. I add > that record/array to an array (2D) in which I store the complete > contents of the file. So... something like: > > $total = array(); > $one_record = array(); > while ($one_record = fgetcsv ($fd, $max_length, $delimiter)) { > array_push ($total, $one_record); > } > > The file I try to read is 1.5M, contains 9040 lines and each line > contains 85 "columns". > Using the 2D construction described above, PHP requires up to 32M as > the memory_limit. And even then, after testing it several times, > that isn't enough. So... My conclusion is that PHP can't really > handle multidimensional arrays very well. > > Reading the complete file into memory using file(), doesn't give any > problems. I can set the memory_limit downto 3M and it still works > fine. > > When I use the fgetcsv() construction described above, I can get it > to work though... But I have to make a minor change for that: > > array_push ($total, implode ("\t", $one_record)); > > That's right... I have to make it a 1D array!!! Then there's nothing > wrong. > > When I use file() and then try to make a 2D array... it doesn't work > either: > > $contents = file (); > foreach ($contents as $record) { > $data = split ("\t", $record); > array_push ($total, $data); > } > > This also produces the memory exhausted error. > > > Some other way to try and process this file (and a dirty one it is!) > is by creating all separate 1D arrays: > > $var_name = "myVar_$counter"; > $$var_name = $data; > $counter++; > > But.... No! Doesn't work. All of a sudden PHP doesn't want to work > with the 1D arrays neither. > > > Can anyone tell me more about this array-bug in PHP? Or even > better... Does anyone know something about a patch for this? > > -- > > * R&zE: > > > -- »»»»»»»»»»»»»»»»»»»»»»»» > -- Renze Munnik > -- DataLink BV > -- > -- E: renze@datalink.nl > -- W: +31 23 5326162 > -- F: +31 23 5322144 > -- M: +31 6 21811143 > -- > -- Stationsplein 82 > -- 2011 LM HAARLEM > -- Netherlands > -- > -- http://www.datalink.nl > -- «««««««««««««««««««««««« > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#70393) next »