nick5449 Posted July 4, 2006 Share Posted July 4, 2006 I have a .csv file (comma separated value) that I want to open, and store in array. I'm not sure how many values will be in the file. Heres the code I have so far but im not sure if it will work.[code] $fp = fopen("quickMessage.csv", "a"); $x = 0; $msgArray = array(); while($x != count($msgarray)) { $msgArray($x) = fgets($fp, 300); $x++; } print_r($msgArray);[/code]Will this work? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/13672-array-help/ Share on other sites More sharing options...
Orio Posted July 4, 2006 Share Posted July 4, 2006 [code=php:0]//php version has to be PHP5 or greater$file=file_get_contents("quickMessage.csv");$arr_file=str_split($file,300);print_r($arr_file);[/code][hr]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/13672-array-help/#findComment-53034 Share on other sites More sharing options...
nick5449 Posted July 4, 2006 Author Share Posted July 4, 2006 Im getting the error: Fatal error: Call to undefined function: str_split()with that code. Also, does that function just separate the values into the array $arr_file? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/13672-array-help/#findComment-53038 Share on other sites More sharing options...
Eugene Posted July 4, 2006 Share Posted July 4, 2006 [code] $fp = fopen("quickMessage.csv", "a"); $x = 0; $msgArray = array(); while($x != count($msgarray)) { $msgArray[$x] = fgets($fp, 300); $x++; } print_r($msgArray);[/code]Try that. Quote Link to comment https://forums.phpfreaks.com/topic/13672-array-help/#findComment-53051 Share on other sites More sharing options...
Barand Posted July 5, 2006 Share Posted July 5, 2006 [code]<?php$msg_array = file('quickMessage.csv');?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13672-array-help/#findComment-53120 Share on other sites More sharing options...
Orio Posted July 5, 2006 Share Posted July 5, 2006 [quote author=nick5449 link=topic=99444.msg391607#msg391607 date=1152047540]Im getting the error: Fatal error: Call to undefined function: str_split()with that code. Also, does that function just separate the values into the array $arr_file? Thanks[/quote]That's because you have PHP<5...Add this to my code:[code=php:0]if(!function_exists('str_split')){ function str_split($string,$split_length=1){ $count = strlen($string); if($split_length < 1){ return false; } elseif($split_length > $count){ return array($string); } else { $num = (int)ceil($count/$split_length); $ret = array(); for($i=0;$i<$num;$i++){ $ret[] = substr($string,$i*$split_length,$split_length); } return $ret; } } };[/code][hr]And this should resolve you probs :)@Barand- he wants to split the array into chuncks of 300 chars, so that wont work.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/13672-array-help/#findComment-53228 Share on other sites More sharing options...
heckenschutze Posted July 5, 2006 Share Posted July 5, 2006 He never said he wants it in 300 byte chunks, you just assumed that because he is only reading in 300 bytes into his array element.. :P Quote Link to comment https://forums.phpfreaks.com/topic/13672-array-help/#findComment-53274 Share on other sites More sharing options...
Barand Posted July 5, 2006 Share Posted July 5, 2006 @Orio,[quote=php manual]fgets(PHP 3, PHP 4, PHP 5)fgets -- Gets line from file pointerDescriptionstring fgets ( resource handle [, int length] )Returns a string of up to length - 1 bytes read from the file pointed to by handle. Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, the length defaults to 1k, or 1024 bytes. [/quote] Quote Link to comment https://forums.phpfreaks.com/topic/13672-array-help/#findComment-53484 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.