Jump to content

array help


nick5449

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/13672-array-help/
Share on other sites

[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.
Link to comment
https://forums.phpfreaks.com/topic/13672-array-help/#findComment-53228
Share on other sites

@Orio,

[quote=php manual]
fgets
(PHP 3, PHP 4, PHP 5)

fgets -- Gets line from file pointer
Description
string 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]
Link to comment
https://forums.phpfreaks.com/topic/13672-array-help/#findComment-53484
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.