Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

An API I am querying returns the following data which I've used json_decode to put into an array. I need to extract certain values for inserting into a MySQL database. In the example below (which only has one ID), I want to insert ID = 4229850 and 2011 (the value of the field Vanguard). I'm can't figure out how to navigate through the array and generate the appropriate insert statement.

Array 
    ( 
    [Contacts] => Array 
        ( 
        [0] => Array 
            ( 
            [Id] => 4229850 [Url] => https://abc.com 
            [FirstName] => Mol 
            [LastName] => Thompson 
            [FieldValues] => Array 
                (
                [0] => Array 
                    (
                    [FieldName] => Profile last updated [Value] => 
                    ) 
                [1] => Array 
                    (
                    [FieldName] => First name [Value] => Mol 
                    [CustomAccessLevel] => Public
                    )
                [2] => Array 
                    (
                    [FieldName] => Last name [Value] => Thompson 
                    )
                [3] => Array 
                    (
                    [FieldName] => e-Mail [Value] => [email protected]
                    )
                [4] => Array 
                    (
                    [FieldName] => Vanguard [Value] => 2011
                    ) 
                )
            )
        ) 
    )
share|improve this question
    
you want to retrieve above mentioned value and insert it to database? –  Joke_Sense10 Oct 19 '13 at 6:31
    
These are two separate questions. Using MySQL with PHP is well documented, as are Arrays (Example #6). –  Tim Oct 19 '13 at 6:32
add comment

1 Answer

$field1=$yourArray['contacts'][0]['Id'] //Value 4229850
$field2=$yourArray['contacts'][0]['FieldValues'][4]['Value'] //Value 2011

Using PDO Methods

$sql = "INSERT INTO yourTable (field1,field2) VALUES (:field1,:field2)";
$q = $conn->prepare($sql);
$q->execute(array(':field1'=>$field1,
                  ':field2'=>$field2));

Using mysql_query function

mysql_query("INSERT INTO yourTable (field1,field2) VALUES ('$field1','$field2')");
share|improve this answer
    
Brilliant. Thanks. It works perfectly. –  user2897132 Oct 19 '13 at 8:12
1  
Happy that it helped. Can you mark this as accepted if it really worked for you? –  Jenson M John Oct 19 '13 at 8:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.