Raz0r Posted November 16, 2009 Share Posted November 16, 2009 Ok guys...I need some help... I do not have idea how to do this: I need PHP code that will open a text document and read it, and store it in the array, in this way. $a=array("1" = "example", "2" = "example2"......., so each line has its number in array...) I hope that this is possible. Thank you. Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/ Share on other sites More sharing options...
joel24 Posted November 16, 2009 Share Posted November 16, 2009 $file_handle = fopen("yourtextdoc.txt", "rb"); $textArray = array(); while (!feof($file_handle) ) { $line_of_text = fgets($file_handle); $textArray[] = $line_of_text; } fclose($file_handle); //now your lines of text are stored in $textArray Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958421 Share on other sites More sharing options...
Raz0r Posted November 16, 2009 Author Share Posted November 16, 2009 Hmmm.... It is not working.When I echo it, it just displays "Array" on screen. Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958426 Share on other sites More sharing options...
cags Posted November 16, 2009 Share Posted November 16, 2009 You cannot just echo an array. If you wish to view the contents you should use something like print_r. Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958430 Share on other sites More sharing options...
salathe Posted November 16, 2009 Share Posted November 16, 2009 The file function will read a file into an array of the lines. Is that what you want? Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958435 Share on other sites More sharing options...
Raz0r Posted November 16, 2009 Author Share Posted November 16, 2009 Thank you, joel24 and cags. You solved my problem. I haven't expected so fast answer. Thank you one more time One more thing... Is it possible to do the same thing, but from textbox? Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958446 Share on other sites More sharing options...
cags Posted November 16, 2009 Share Posted November 16, 2009 Yes, but that would depend on how you wished to split the data into an array. You could simply explode the data on \r\n, which would give a similar result to reading it from a file. Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958455 Share on other sites More sharing options...
Raz0r Posted November 16, 2009 Author Share Posted November 16, 2009 One more question... How do i send mail to addresses in array? Let's say i have this: <?php $to = I need addresses from array to be here. $subject = 'the subject'; $message = 'hello'; $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); ?> Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958467 Share on other sites More sharing options...
cags Posted November 16, 2009 Share Posted November 16, 2009 Personally I wouldn't put them where you have marked. $emails = array("[email protected]", "[email protected]", "[email protected]"); $to = $emails[0]; unset($emails[0]); $bcc = implode("; ", $emails); $headers = 'From: [email protected]' . "\r\n"; $headers .= 'Reply-To: [email protected]' . "\r\n"; $headers .= 'BCC: ' . $bcc; $headers .= 'X-Mailer: PHP/' . phpversion(); Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958474 Share on other sites More sharing options...
Raz0r Posted November 16, 2009 Author Share Posted November 16, 2009 WOW MAN!!! I got it now!!! Some things are a lot of clearer to me now... Thank you!!!!!!!!!!!! Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958631 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.