Jump to content

Recommended Posts

I have created a similar multidimensional array (as shown below) and would like to know the best way to add this to a mysql database using an INSERT string.

$array = array
(
  "0"=>array
  (
        "0"=>array (
                        "Date",
                        "ID",
                        "Family"
                 ),
        "1"=>array (
                        "Date",
                        "ID",
                        "Family"
                 )
  ),
  "1"=>array
  (
        "0"=>array (    "Date",
                        "ID",
                        "Family"
                 ),
        "1"=>array (
                        "Date",
                        "ID",
                        "Family"
                 )
  ),
  "2"=>array
  (
        "0"=>array (
                        "Date",
                        "ID",
                        "Family"
                 ),
        "1"=>array (
                        "Date",
                        "ID",
                        "Family"
                 )
  )
);

 

I understand i could use the implode() function to create a string, but i have had trouble using it on a 3D array with a double foreach() function to provide a string which looks like this:

 

( ($value1,$value2,$value3),($value1,$value2,$value3),($value1,$value2,$value3) );

 

rather i get (frustratingly) something like this:

 

$value1 $value2 $value3 $value1 $value2 $value3 $value1 $value2 $value3 etc...

 

Essentially in a nutshell I need help to insert a 3D array into a database table with 3 columns!!!

 

Any help greatly appreciated!!!

use serialize() and unserialize() functions

try

<?php
$array = array
(
  "0"=>array
  (
        "0"=>array (
                        "Date",
                        "ID",
                        "Family"
                 ),
        "1"=>array (
                        "Date",
                        "ID",
                        "Family"
                 )
  ),
  "1"=>array
  (
        "0"=>array (    "Date",
                        "ID",
                        "Family"
                 ),
        "1"=>array (
                        "Date",
                        "ID",
                        "Family"
                 )
  ),
  "2"=>array
  (
        "0"=>array (
                        "Date",
                        "ID",
                        "Family"
                 ),
        "1"=>array (
                        "Date",
                        "ID",
                        "Family"
                 )
  )
);
$ser_arr = serialize($array);
print_r($ser_arr);
$unser_arr = unserialize($ser_arr);
echo "\n<br />\n";
print_r($unser_arr);
?>

I'm going to question how you generate this array because the insertion might be best if the creation is known (I'm assuming its coming from Post or Get or cURL or some sort and not a static array, because if its static I must ask why its static in the first place.

try

<?php
$array = array
(
  "0"=>array
  (
        "0"=>array (
                        "Date",
                        "ID",
                        "Family"
                 ),
        "1"=>array (
                        "Date",
                        "ID",
                        "Family"
                 )
  ),
  "1"=>array
  (
        "0"=>array (    "Date",
                        "ID",
                        "Family"
                 ),
        "1"=>array (
                        "Date",
                        "ID",
                        "Family"
                 )
  ),
  "2"=>array
  (
        "0"=>array (
                        "Date",
                        "ID",
                        "Family"
                 ),
        "1"=>array (
                        "Date",
                        "ID",
                        "Family"
                 )
  )
);

foreach ($array as $a)
{
    foreach ($a as $b)
    {
            $data[] = "('". join("','", $b)."')";
    }
}

$sql = "INSERT INTO table (date,id,family) VALUES \n" . join(",\n", $data);
echo '<pre>', $sql, '</pre>';
?>

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.