青春有我
您的 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)