...

View Full Version : Array data to MySQL



rf9
07-17-2008, 02:55 PM
Hi all,
I've got an array of data and I need to figure out how to deal with it and insert it into a database. The array looks like this:

array(2) {
[0]=> array(3) {
["labelName"]=> string(9) "First name"
["inputType"]=> string(22) "textfield"
["validate"]=> string(7) "no" }
[1]=> array(3) {
["labelName"]=> string(9) "Last name"
["inputType"]=> string(28) "textfield"
["validate"]=> string(7) "no" } }

So the array above would create 2 rows in a database. I have dealt with arrays before but this one is a little more complex to me. I guess some kind of loop will be needed to get the values and then pass them to an insert query. Any advice much appreciated.

Many thanks in advance.

oesxyl
07-17-2008, 03:13 PM
try this:


function extr($a){
return ('('.join(",",array_values($a)).')');
}
$values = join(',',array_map("extr",$oldarr));


not tested, but if is correct is must return something like this:


("First name", "textfield", "no"), ("Last name", "textfield", "no")


regards

rf9
07-17-2008, 04:44 PM
Hi again :)
Thanks for the help, the code works great and outputs as you expected.

Just to clarify, $a is my array.
Not sure about $oldarr, I replaced this with $a and it works fine.

I just need to figure out the query now and it should be sorted.

oesxyl
07-17-2008, 05:30 PM
Hi again :)
Thanks for the help, the code works great and outputs as you expected.

Just to clarify, $a is my array.
Not sure about $oldarr, I replaced this with $a and it works fine.

I just need to figure out the query now and it should be sorted.
no, $oldarr is your array. $a is the argument for extr function, you don't use it.
$values is last part of the query( see the thread from mysql forum).



$query = 'insert into yourtablename (labelName, inputType, validate) values '.$values;
echo $query;


regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum