PHP json数组,并从深度嵌套的数组中获取一些键值
我有一个从API获取的JSON数据,我想为每个属性获取/访问
code
和values
要存储到表中的代码,因为表列和值对于每个记录都是动态的。我不需要存储json值中的附件和Total。
我想以INSERT INTO(ID,TrackingNumber,CustomerFull)值(json值)的形式存储到表中。任何代码引爆器、php解决方案或提示都将非常有用。
{
"Values": [{
"AttributeValues": [{
"ID": 0,
"Name": "ID",
"Values": [
"720"
],
"DataType": "Number",
"Code": "ID",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
},
{
"ID": 0,
"Name": "Tracking Number",
"Values": [
"xxxx/1094/180730/1"
],
"DataType": "String",
"Code": "TrackingNumber",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
},
{
"ID": 0,
"Name": "Customer Full",
"Values": [
"Ali Abdi"
],
"DataType": "String",
"Code": "CustomerFull",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
}
],
"Attachements": null
},
{
"AttributeValues": [{
"ID": 0,
"Name": "ID",
"Values": [
"757"
],
"DataType": "Number",
"Code": "ID",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
},
{
"ID": 0,
"Name": "Tracking Number",
"Values": [
"xxx/1094/180731/1"
],
"DataType": "String",
"Code": "TrackingNumber",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
},
{
"ID": 0,
"Name": "Customer Full",
"Values": [
"Aberash Haile"
],
"DataType": "String",
"Code": "CustomerFull",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
},
{
"ID": 0,
"Name": "Service Code",
"Values": [
"SO-1096"
],
"DataType": "String",
"Code": "ServiceCode",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
},
{
"ID": 0,
"Name": "Request Date",
"Values": [
"7/31/2018 11:04:06 AM"
],
"DataType": "Datetime",
"Code": "RequestDate",
"IsVisible": true,
"IsValueEmptyOrNull": false,
"IsDefault": true
}
],
"Attachements": null
}
],
"Total": 335
}
JSON
首先,您应该将推荐答案转换为数组,然后使用循环(Foreach、For、While...)并获得相应的价值,如下所示:
<?php
$json = '{
"Values":[
{
"AttributeValues":[
{
"ID":0,
"Name":"ID",
"Values":[
"720"
],
"DataType":"Number",
"Code":"ID",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
},
{
"ID":0,
"Name":"Tracking Number",
"Values":[
"xxxx/1094/180730/1"
],
"DataType":"String",
"Code":"TrackingNumber",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
},
{
"ID":0,
"Name":"Customer Full",
"Values":[
"Ali Abdi"
],
"DataType":"String",
"Code":"CustomerFull",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
}
],
"Attachements":null
},
{
"AttributeValues":[
{
"ID":0,
"Name":"ID",
"Values":[
"757"
],
"DataType":"Number",
"Code":"ID",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
},
{
"ID":0,
"Name":"Tracking Number",
"Values":[
"xxx/1094/180731/1"
],
"DataType":"String",
"Code":"TrackingNumber",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
},
{
"ID":0,
"Name":"Customer Full",
"Values":[
"Aberash Haile"
],
"DataType":"String",
"Code":"CustomerFull",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
},
{
"ID":0,
"Name":"Service Code",
"Values":[
"SO-1096"
],
"DataType":"String",
"Code":"ServiceCode",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
},
{
"ID":0,
"Name":"Request Date",
"Values":[
"7/31/2018 11:04:06 AM"
],
"DataType":"Datetime",
"Code":"RequestDate",
"IsVisible":true,
"IsValueEmptyOrNull":false,
"IsDefault":true
}
],
"Attachements":null
}
],
"Total":335
}';
$json_data = json_decode($json, true);
//echo "<pre>"; print_r($json_data['Values']); exit;
if(isset($json_data['Values'])){
foreach($json_data as $data){
foreach($data as $attr){
if(isset($attr['AttributeValues'])){
foreach($attr['AttributeValues'] as $value){
if(isset($value['Values'])){
foreach($value['Values'] as $val){
$attribute_values = $val;
echo "<pre>"; print_r($attribute_values); echo "</pre>";
}
}
}
}
}
}
}
?>
相关文章