JSON 中的动态变量以 PHP 显示

我试图查找线索,但仍然无法解决。

我有这样的json,我需要它在db中存储以下'id','n','ct'。但是元素“rep”有不同的编号。谁能帮我创建代码“foreach”以便能够调用它然后保存它?基本上我需要用它的“id”为每个“rep”获取结果

如:

rep1 - id 1,2,4,5

代表 2 - id 1,2

{       "dataFlags": 8192,

        "totalItemsCount": 6,

        "indexFrom": 0,

        "indexTo": 0,

        "items": [{

                "rep": {

                    "1": {

                        "id": 1,

                        "n": "volvo",

                        "ct": "avl_unit_group",

                        "c": 54071

                    },

                    "2": {

                        "id": 2,

                        "n": "bmw",

                        "ct": "avl_unit_group",

                        "c": 59631

                    },

                    "4": {

                        "id": 4,

                        "n": "audi",

                        "ct": "avl_unit_group",

                        "c": 27264

                    },

                    "5": {

                        "id": 5,

                        "n": "mercedes",

                        "ct": "avl_unit",

                        "c": 18276

            }}},

            {"rep": {

                    "1": {

                        "id": 1,

                        "n": "tesla",

                        "ct": "avl_unit",

                        "c": 24132

                    },

                    "2": {

                        "id": 2,

                        "n": "scania",    

                        "ct": "avl_unit",

                        "c": 2178

                    }},

                "repmax": 0

}]}


慕森王
浏览 346回答 3
3回答

青春有我

您的 JSON 数据:&nbsp;$jsonData = '{&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "dataFlags": 8192,&nbsp; &nbsp; &nbsp; &nbsp; "totalItemsCount": 6,&nbsp; &nbsp; &nbsp; &nbsp; "indexFrom": 0,&nbsp; &nbsp; &nbsp; &nbsp; "indexTo": 0,&nbsp; &nbsp; &nbsp; &nbsp; "items": [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "rep": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "1": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "n": "volvo",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ct": "avl_unit_group",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "c": 54071&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },......&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {"rep": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "1": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "n": "tesla",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ct": "avl_unit",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "c": 24132&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },..........&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "repmax": 0}]}';试试下面的代码。它正在工作。我使用 2foreach 将您的 JSON 数据存储在数据库中。$jsonData = json_decode($jsonData,true);foreach($jsonData['items'] as $items){&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;foreach($items['rep'] as $rep){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $sql = "INSERT INTO MyGuests (id, n, ct) VALUES&nbsp; &nbsp;(".$rep['id'].",".$rep['n'].",".$rep['ct'].")";&nbsp; &nbsp; &nbsp; &nbsp; $conn->query($sql)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}获取每个“rep”及其“id”的结果:$jsonData = json_decode($jsonData,true);$allRep = [];foreach($jsonData['items'] as $items){&nbsp; &nbsp; $tmpRep = [];&nbsp; &nbsp; foreach($items['rep'] as $key => $rep){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $tmpRep [] = $key;&nbsp; &nbsp; }&nbsp; &nbsp; $allRep[] = implode(', ',$tmpRep);}echo "<pre>";print_r($allRep);输出:Array(&nbsp; &nbsp; [0] => 1, 2, 4, 5&nbsp; &nbsp; [1] => 1, 2)

12345678_0001

这段代码是用 php 编写的。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$rep1="";&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; foreach ($row['item'] as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rep1 .=$value->id.'-';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $rep1=substr($rep1, 0, -1);输出将是 $rep1= 1-2-4-5

婷婷同学_

检查这个:$arr = json_decode($yourJSONstring);foreach($arr->items as $index=>$rep){&nbsp; &nbsp; foreach($rep->rep as $element){&nbsp; &nbsp; &nbsp; &nbsp; $reps[$index][] = $element->id;&nbsp; &nbsp; }}代表数组将如下所示:Array (&nbsp;&nbsp; &nbsp;[0] => Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 5 )&nbsp;&nbsp; &nbsp;[1] => Array ( [0] => 1 [1] => 2 )&nbsp;)因此,您可以根据需要轻松将其转换为字符串格式。
打开App,查看更多内容
随时随地看视频慕课网APP