Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    rf9
    rf9 is offline
    New Coder
    Join Date
    Jun 2008
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Array data to MySQL

    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:
    Code:
    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.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try this:
    PHP Code:
    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

  • Users who have thanked oesxyl for this post:

    rf9 (07-17-2008)

  • #3
    rf9
    rf9 is offline
    New Coder
    Join Date
    Jun 2008
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by rf9 View Post
    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).

    PHP Code:
    $query 'insert into yourtablename (labelName, inputType, validate) values '.$values;
    echo 
    $query
    regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •