为了账号安全,请及时绑定邮箱和手机立即绑定

JSON 中的动态变量以 PHP 显示

JSON 中的动态变量以 PHP 显示

PHP
慕森王 2022-06-17 10:29:33
我试图查找线索,但仍然无法解决。我有这样的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}]}
查看完整描述

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

)


查看完整回答
反对 回复 2022-06-17
?
12345678_0001

TA贡献1802条经验 获得超5个赞

这段代码是用 php 编写的。


         $rep1="";  

        foreach ($row['item'] as $key => $value) {

            $rep1 .=$value->id.'-';

        }

        $rep1=substr($rep1, 0, -1);

输出将是 $rep1= 1-2-4-5


查看完整回答
反对 回复 2022-06-17
?
婷婷同学_

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 ) 

)

因此,您可以根据需要轻松将其转换为字符串格式。


查看完整回答
反对 回复 2022-06-17
  • 3 回答
  • 0 关注
  • 345 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号