亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 310 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號