创建了DB记录,但是没有插入具有键值对数据的数组 - json php

问题描述 投票:-1回答:1

将以下json数组数据提取到php脚本,最终在MySQL数据库中创建记录:

{"address": "4 Ficticious Ave", 
"city": "Miami", 
"country": "United States", 
"email": "[email protected]", 
"first_name": "Jane", 
"last_name": "Doe", 
"state": "FL", 
"zip_code": "03423", 
"response_data": 
"[{"key":"7122", "value":"37-52"},
{"key":"7123","value":"Female"},
{"key":"7124","value":"$35,000 to $50,000 USD"},
{"key":"6176","value":"Miami"},
{"key":"6177","value":"FL"},
{"key":"6179","value":"United States"}]"}

我已经获得了以下PHP脚本来从json数组获取数据并通过插入数据在MySQL数据库中创建记录。但是,除了来自response_data键值对的数据之外的所有数据都会被填充 - 关联的MySQL列都为空:

// Identify the content as json
header("Content-Type: application/json; charset=UTF-8");

// get the contents of the JSON file
$data = file_get_contents("php://input");


//this normalize routine was provided by @Elementary in 
// response to my request on Stack Overflow 09052018...

//begin normalize the json in order to be properly decoded
$start=strpos($data,':',strpos($data,'response_data'));
$get=substr($data,$start+1,strrpos($data,'"')-$start);
$data=str_replace($get,trim(trim($get),'"'),$data);
//end normalize


//decode JSON data to PHP array
$content = json_decode($data, true);

//Fetch the details of customer
$Cust_Fname = $content['first_name'];
$Cust_Lname = $content['last_name'];
$Cust_Email = $content['email'];
$Street_Address = $content['address'];
$City = $content['city'];
$State = $content['state'];
$Country = $content['country'];
$Postal_Code = $content['zip_code'];

//also fetch the appended "array" of key/value fields...
$Response_AgeKey = $content['reponse_data'][0]['key'];
$Response_GenderKey = $content['reponse_data'][1]['key'];
$Response_IncomeKey = $content['reponse_data'][2]['key'];
$Response_CityKey = $content['reponse_data'][3]['key'];
$Response_StateKey = $content['reponse_data'][4]['key'];
$Response_CountryKey = $content['reponse_data'][5]['key'];
$Response_Age = $content['reponse_data'][0]['value'];
$Response_Gender = $content['reponse_data'][1]['value'];
$Response_Income = $content['reponse_data'][2]['value'];
$Response_City = $content['reponse_data'][3]['value'];
$Response_State = $content['reponse_data'][4]['value'];
$Response_Country = $content['reponse_data'][5]['value'];

MySQL数据库显示已创建的记录,并包含除来自response_data的数据之外的所有数据字段。认为我的语法可能有问题,我试图用这个替换response_data变量:

//try this syntax instead…
$Response_AgeKey = $content['reponse_data']['key'][0];
$Response_GenderKey = $content['reponse_data']['key'][1];
$Response_IncomeKey = $content['reponse_data']['key'][2];
$Response_CityKey = $content['reponse_data']['key'][3];
$Response_StateKey = $content['reponse_data']['key'][4];
$Response_CountryKey = $content['reponse_data']['key'][5];
$Response_Age = $content['reponse_data']['value'][0];
$Response_Gender = $content['reponse_data']['value'][1];
$Response_Income = $content['reponse_data']['value'][2];
$Response_City = $content['reponse_data']['value'][3];
$Response_State = $content['reponse_data']['value'][4];
$Response_Country = $content['reponse_data']['value'][5];

获得相同的结果 - 在MySQL数据库中创建记录,但response_data数组字段不填充关联的MySQL列。我可以帮助学习一些其他方法来识别和获取response_data数组中的数据。请注意,我不想将response_data数组作为json数组插入MySQL - 相反,数组中的数据应该进入相关的MySQL列!

php json mysqli associative-array keyvaluepair
1个回答
0
投票

我已经解决了这个问题,现在我可以从json数组中获取所有数据,以插入到MySql中的相关列中。解决方案涉及将'response_data'的细节提取到php中的关联数组中:

//also fetch the appended "array" of key/value fields...
$Response_Array = $content['response_data'];

然后,我查看了php数组以验证传递的数据并注意结构:

//look at the array to verify data is fetched
var_dump($Response_Array);

最后,我将键值对的值带入变量:

//bring the values of response_data array into variables 
$Response_Age = $Response_Array[0]['value'];
$Response_Gender = $Response_Array[1]['value'];
$Response_Income = $Response_Array[2]['value'];
$Response_City = $Response_Array[3]['value'];
$Response_State = $Response_Array[4]['value'];
$Response_Country = $Response_Array[5]['value'];

现在,当创建MySQL记录时,将根据需要插入所有数据[未提供MySQL插入代码和错误例程]。

感谢所有帮助,让我朝着我需要解决的方向前进。

© www.soinside.com 2019 - 2024. All rights reserved.