applebiz89 Posted April 4, 2009 Share Posted April 4, 2009 firstly, I want to break down for example, a text file, so it reads word by word in to an array. I then want to add this array to an sql database. I am using explode to use this so this makes the arrray, but when I insert it to the database it comes out just as 'array array array' and not the contents of the file. if i echo the explode array it comes out with the contents in the browser. Is there a way to put the results from the explode into a new array that can be inserted into the database. this is my code function opening($file) { $pattern = "(\.txt$)| (\.php$)"; if(eregi($pattern, $file)){ $file_handle=fopen($file, "rb"); while (!feof($file_handle)){ $line_of_text = fgets($file_handle); $parts = explode(" ", $line_of_text); printing($parts); } fclose($file_handle);} else { echo "Cant open that file type <p>"; } } function newarray($parts){ $data = array(); for($i = 0; $i < count($parts); ++$i) $data[] = $parts; } I am trying to make a function to put this wanted data into a new array, but not sure how. obviously need a loop Any help would be great. cheers Quote Link to comment https://forums.phpfreaks.com/topic/152563-help-needed-with-array/ Share on other sites More sharing options...
charleshill Posted April 4, 2009 Share Posted April 4, 2009 Well if you're trying to store an array in a database table, you need to turn it into a string. I use serialize() to convert complex or associative arrays into a string before insertion into a database table. You will have to use unserialize() on the string when you retrieve it from the database table to convert it back to its array form. Quote Link to comment https://forums.phpfreaks.com/topic/152563-help-needed-with-array/#findComment-801286 Share on other sites More sharing options...
applebiz89 Posted April 4, 2009 Author Share Posted April 4, 2009 ok thanks. so I dont need to make a new array I just use serialize? this is just a small segment from my code, but i have used explode when reading in the contents of the file. is this how you would use serialize? $parts = explode(" ", $line_of_text); $data = array(serialize($parts)); Quote Link to comment https://forums.phpfreaks.com/topic/152563-help-needed-with-array/#findComment-801290 Share on other sites More sharing options...
charleshill Posted April 4, 2009 Share Posted April 4, 2009 If all you want is to read the contents of a file into an array... just use file() You only need to serialize the resulting array if you want to insert it into a database table. Quote Link to comment https://forums.phpfreaks.com/topic/152563-help-needed-with-array/#findComment-801317 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.