Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.