RE: [PHP] Array Creation
-Original Message- From: Bogdan Stancescu [mailto:[EMAIL PROTECTED]] Sent: 09 January 2002 20:56 while (!feof ($fp)) { $line = fgets($fp, 4096); $data = explode(,, str_replace(\, , $line)); //using str_replace to get rid of quote marks //following line to see if data is correct echo $data[0]. .$data[1].br; $theArray[$data[0]]=$data[1]; } fclose($fp); You should make sure you don't accidentally delete some double quotes inside the strings - maybe explode(,,substr($line,1,strlen($line)-2)); would be a better option. explode(,, substr($line, 1, -2)); would be even better! Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Array Creation
while (!feof ($fp)) { $line = fgets($fp, 4096); $data = explode(,, str_replace(\, , $line)); //using str_replace to get rid of quote marks //following line to see if data is correct echo $data[0]. .$data[1].br; $theArray[$data[0]]=$data[1]; } fclose($fp); You should make sure you don't accidentally delete some double quotes inside the strings - maybe explode(,,substr($line,1,strlen($line)-2)); would be a better option. Bogdan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Array Creation
Also, make sure you get rid of the space at the beginning of $data[1] - the space after the comma gets in there, so either use trim() or just trim the first character programatically ($data[1]=substr($data[1],1)). Bogdan Jamison Roberts wrote: What I have is a text file that has data in the following format: username, numbers The quotation markes can't be helped...the script I use to generate the file adds them automatically. (Hummingbird Basic, one of the worst languages in the history of mankind.) What I've been trying to do is create an array from the file with the following format: array (username=numbers) I have gotten this far: while (!feof ($fp)) { $line = fgets($fp, 4096); $data = explode(,, str_replace(\, , $line)); //using str_replace to get rid of quote marks //following line to see if data is correct echo $data[0]. .$data[1].br; } fclose($fp); The data is right, however I can't seem to build an array dynamically. Also, please let me know if I can do this more efficiently. Thanks. NOTE: Using php 4.0.6, so the function trim($line, \) did not work ** Jamison R. Website: http://www.nuitari.com ICQ: 2375136 MSN Messenger: einstruzende at hotmail.com ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]