php json array of arrays 并从深层嵌套中获取一些键值
php json array of arrays and get some key values from deep nested
我有一个从 API 获取的 JSON 数据,我想 get/access 为每个 AttributeValues code
和 values
存储到表,代码作为表的列和值对于每个记录都是动态的。我不需要存储 json 值中的 Attachements 和 Total。
我想存储到表中作为插入到 (ID、TrackingNumber、CustomerFull) 值(json 值)。任何 codeigniter 或 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>";
}
}
}
}
}
}
}
?>
我有一个从 API 获取的 JSON 数据,我想 get/access 为每个 AttributeValues code
和 values
存储到表,代码作为表的列和值对于每个记录都是动态的。我不需要存储 json 值中的 Attachements 和 Total。
我想存储到表中作为插入到 (ID、TrackingNumber、CustomerFull) 值(json 值)。任何 codeigniter 或 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>";
}
}
}
}
}
}
}
?>