Jump to content

help needed with array


applebiz89

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/152563-help-needed-with-array/
Share on other sites

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.

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));

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.