创建了数据库记录,但未插入具有键值对数据的数组-JSON PHP

时间:2018-09-06 07:41:19

标签: php json mysqli associative-array keyvaluepair

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

{"address": "4 Ficticious Ave", 
"city": "Miami", 
"country": "United States", 
"email": "jane_doe@gmail.com", 
"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列中!

1 个答案:

答案 0 :(得分: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插入代码和错误例程]。

谢谢大家的帮助,这为我提供了解决方向。