Jump to content

[SOLVED] Multidimensional Array Manipulation


zoonose

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>';
?>

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.