我正在嘗試從 Invision Community REST API 調(diào)用的 Json 響應(yīng)內(nèi)部訪問“結(jié)果”數(shù)組。我不確定如何從 PHP 代碼中過濾到結(jié)果數(shù)組,如下所示。一旦我過濾到它,我想將它回顯到屏幕上,就像當(dāng)前顯示完整響應(yīng)的方式一樣。任何幫助將不勝感激!PHP代碼<?php $communityUrl = 'https://website.net/invisioncommunity'; $apiKey = 'apikey'; $endpoint = '/core/members'; $curl = curl_init( $communityUrl . 'api' . $endpoint ); curl_setopt_array( $curl, array( CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => "{$apiKey}:" ) ); $response = curl_exec( $curl ); $resArr = array(); $resArr = json_decode($response); $resultsArr = cleanResponse($response); echo "<pre>"; print_r($resultsArr); echo "</pre>"; function cleanResponse($arr) { $element = $arr['results']; return $element; }API 調(diào)用的 Json(部分)響應(yīng){ "page": 1, "perPage": 25, "totalResults": 111, "totalPages": 5, "results": [ { "id": 1, "name": "Demo", "title": null, "timeZone": "America\/New_York", "formattedName": "Demo<\/span>", "primaryGroup": { "id": 4, "name": "Demo", "formattedName": "Demo<\/span>" }, "secondaryGroups": [ { "id": 15, "name": "Root Admin (Do not edit!)", "formattedName": "Root Admin (Do not edit!)" } ], "email": "demo@website.net", "joined": "2020-07-07T19:48:33Z", "registrationIpAddress": "", "warningPoints": 0, "reputationPoints": 6, "photoUrl": "", "photoUrlIsDefault": false, "coverPhotoUrl": "", "profileUrl": "https:\/\/www.website.net\/profile\/1-demo\/", "validating": false, "posts": 3,
1 回答

繁華開滿天機
TA貢獻1816條經(jīng)驗 獲得超4個贊
對代碼進行一些小更改應(yīng)該可以解決您的問題:
要獲取返回的關(guān)聯(lián)數(shù)組,請用作
true
第二個參數(shù)json_decode()
$resArr = json_decode($response, true);
使用關(guān)聯(lián)數(shù)組
$resArr
作為函數(shù)的參數(shù)cleanResponse
:
$resultsArr = cleanResponse($resArr);
現(xiàn)在$resultsArr
保存results
子數(shù)組,您可以使用例如輸出print_r()
- 1 回答
- 0 關(guān)注
- 132 瀏覽
添加回答
舉報
0/150
提交
取消