Jump to content


Photo

array help


  • Please log in to reply
7 replies to this topic

#1 nick5449

nick5449
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 04 July 2006 - 08:56 PM

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.
  	$fp = fopen("quickMessage.csv", "a");
	$x = 0;
	$msgArray = array();
	while($x != count($msgarray)) {
		$msgArray($x) = fgets($fp, 300);
		$x++;
	}
	print_r($msgArray);

Will this work? Thanks

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 04 July 2006 - 09:02 PM

//php version has to be PHP5 or greater
$file=file_get_contents("quickMessage.csv");
$arr_file=str_split($file,300);
print_r($arr_file);
 

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 nick5449

nick5449
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 04 July 2006 - 09:12 PM

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

#4 Eugene

Eugene
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 04 July 2006 - 10:15 PM

  	$fp = fopen("quickMessage.csv", "a");
	$x = 0;
	$msgArray = array();
	while($x != count($msgarray)) {
		$msgArray[$x] = fgets($fp, 300);
		$x++;
	}
	print_r($msgArray);

Try that.

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 05 July 2006 - 01:09 AM

<?php
$msg_array = file('quickMessage.csv');
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 05 July 2006 - 08:51 AM

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

That's because you have PHP<5...
Add this to my code:
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; 
       }      
   }  
};
 
And this should resolve you probs :)

@Barand- he wants to split the array into chuncks of 300 chars, so that wont work.

Orio.
Think you're smarty?

(Gone until 20 to November)

#7 heckenschutze

heckenschutze
  • Members
  • PipPipPip
  • Advanced Member
  • 257 posts
  • LocationAustralia

Posted 05 July 2006 - 11:37 AM

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

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 05 July 2006 - 06:27 PM

@Orio,

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.


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users