zoonose Posted August 29, 2007 Share Posted August 29, 2007 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/67251-solved-multidimensional-array-manipulation/ Share on other sites More sharing options...
sasa Posted August 29, 2007 Share Posted August 29, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/67251-solved-multidimensional-array-manipulation/#findComment-337353 Share on other sites More sharing options...
cooldude832 Posted August 29, 2007 Share Posted August 29, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/67251-solved-multidimensional-array-manipulation/#findComment-337354 Share on other sites More sharing options...
Barand Posted August 29, 2007 Share Posted August 29, 2007 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/67251-solved-multidimensional-array-manipulation/#findComment-337376 Share on other sites More sharing options...
zoonose Posted August 31, 2007 Author Share Posted August 31, 2007 Thanks a lot guys for helping out! Coolduue & Sasa! $sen = array([0]=>'ile',[1]=>'sei'); $Barand = $sen[1]; Thats about it, will have a look at trying these solutions. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/67251-solved-multidimensional-array-manipulation/#findComment-338433 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.