Jump to content

Archived

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

nick5449

array help

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

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
[code]<?php
$msg_array = file('quickMessage.csv');
?>
[/code]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.