3 回答
TA贡献1784条经验 获得超8个赞
您的 JSON 数据:
$jsonData = '{
"dataFlags": 8192,
"totalItemsCount": 6,
"indexFrom": 0,
"indexTo": 0,
"items": [{
"rep": {
"1": {
"id": 1,
"n": "volvo",
"ct": "avl_unit_group",
"c": 54071
},......
}},
{"rep": {
"1": {
"id": 1,
"n": "tesla",
"ct": "avl_unit",
"c": 24132
},..........
},
"repmax": 0
}]}';
试试下面的代码。它正在工作。我使用 2foreach 将您的 JSON 数据存储在数据库中。
$jsonData = json_decode($jsonData,true);
foreach($jsonData['items'] as $items){
foreach($items['rep'] as $rep){
$sql = "INSERT INTO MyGuests (id, n, ct) VALUES (".$rep['id'].",".$rep['n'].",".$rep['ct'].")";
$conn->query($sql)
}
}
获取每个“rep”及其“id”的结果:
$jsonData = json_decode($jsonData,true);
$allRep = [];
foreach($jsonData['items'] as $items){
$tmpRep = [];
foreach($items['rep'] as $key => $rep){
$tmpRep [] = $key;
}
$allRep[] = implode(', ',$tmpRep);
}
echo "<pre>";
print_r($allRep);
输出:
Array
(
[0] => 1, 2, 4, 5
[1] => 1, 2
)
TA贡献1802条经验 获得超5个赞
这段代码是用 php 编写的。
$rep1="";
foreach ($row['item'] as $key => $value) {
$rep1 .=$value->id.'-';
}
$rep1=substr($rep1, 0, -1);
输出将是 $rep1= 1-2-4-5
TA贡献1844条经验 获得超8个赞
检查这个:
$arr = json_decode($yourJSONstring);
foreach($arr->items as $index=>$rep){
foreach($rep->rep as $element){
$reps[$index][] = $element->id;
}
}
代表数组将如下所示:
Array (
[0] => Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 5 )
[1] => Array ( [0] => 1 [1] => 2 )
)
因此,您可以根据需要轻松将其转换为字符串格式。
- 3 回答
- 0 关注
- 345 浏览
添加回答
举报
