我正在查询的API返回以下数据,我使用json_decode将其放入数组中。我需要提取某些值以插入MySQL数据库。在下面的示例中(只有一个ID),我想插入ID = 4229850和2011(字段Vanguard的值)。我无法弄清楚如何在数组中导航并生成适当的插入语句。
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] => abc@yahoo.ca
)
[4] => Array
(
[FieldName] => Vanguard [Value] => 2011
)
)
)
)
)
答案 0 :(得分:2)
$field1=$yourArray['contacts'][0]['Id'] //Value 4229850
$field2=$yourArray['contacts'][0]['FieldValues'][4]['Value'] //Value 2011
使用PDO方法
$sql = "INSERT INTO yourTable (field1,field2) VALUES (:field1,:field2)";
$q = $conn->prepare($sql);
$q->execute(array(':field1'=>$field1,
':field2'=>$field2));
使用mysql_query函数
mysql_query("INSERT INTO yourTable (field1,field2) VALUES ('$field1','$field2')");