如何用它的名字来分隔两个数组

我正在获取 json 中的数组


"sub": {

            "insert_id": [

                161,

                162,

                163,

                164

            ],

            "venue_id": [

                "21",

                "3",

                "5",

                "6"

            ]

        }

像这样,我现在把它放到php变量中,我想要$allidinsert_id :[161,162,163,164] and venue_id :[21,3,5,6]


ABOUTYOU
浏览 79回答 3
3回答

倚天杖

您需要先使用,然后才能以所需的任何方式使用数据json_decode示例(注意:不是最干净的做事方式):/* Decode JSON */$data = '{"sub":{"insert_id":[161,162,163,164],"venue_id":["21","3","5","6"]}}';$subData = json_decode($data,true);/* Just Loop Through Values */foreach ($subData['sub']['insert_id'] as $sub){&nbsp; &nbsp; echo ("<li>$sub</li>");}/* Create New Json */$insertIdJson = json_encode($subData['sub']['insert_id']);$venueIdJson = json_encode($subData['sub']['venue_id']);echo ("Insert IDs: $insertIdJson </br>");echo ("Venue IDs :$venueIdJson </br>");/* Create Array Of insert_id */$insertIdArray = json_decode(json_encode($subData['sub']['insert_id']),true);&nbsp; &nbsp;$venueIdArray = json_decode(json_encode($subData['sub']['venue_id']),true);&nbsp;var_dump($insertIdArray);echo("</br>");var_dump($venueIdArray);

梵蒂冈之花

让我们考虑$json结果变量中的 json 响应$json_result = "sub": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "insert_id": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 161,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 162,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 163,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 164&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "venue_id": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "21",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "3",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "6"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }$result = json_decode($json_result, true);$insert_id = $result["sub"]["insert_id"];$venue_id = $result["sub"]["venue_id"];echo "insert_id";print_r($insert_id);echo "venue_id";print_r($venue_id);

交互式爱情

you can use this.$jsonData = '{"sub":{"insert_id":[161,162,163,164],"venue_id":["21","3","5","6"]}}';$json_result = json_decode($jsonData,true);$insertId = $json_result["sub"]["insert_id"];$venueId = $json_result["sub"]["venue_id"];echo "<pre>";print_r($insertId);echo "<br>";print_r($venueId);
打开App,查看更多内容
随时随地看视频慕课网APP