我正在學習 php,我正在嘗試使用 Guzzle 從 http 請求中獲取 php 文件,然后從這些內容中獲取特定的數組并忽略其余的。但我不確定如何獲取該數組。<?php require_once "vendor/autoload.php";use GuzzleHttp\Client;$client = new Client();$response = $client->request('GET', 'http://api-address/file.php');$body = $response->getBody();$decoded = json_decode($body, true);$contents = file_get_contents($decoded);當我打印 $contents 時,它看起來符合預期,例如:<?php$var = "example string";$array = [ [ 'name' => 'stuff I need' ]];我只想獲取數組,迭代它并將每個寫入文件,但我不知道如何獲取該數組并忽略該數組中不存在的其他內容(如果有意義的話)。任何幫助,將不勝感激。
1 回答

開心每一天1111
TA貢獻1836條經驗 獲得超13個贊
您正在使用 ...
json_decode($body, true);
該方法將內容轉換為數組,這意味著如果您執行...
var_dump($decoded);
你可以看到一個數組,里面包含數組......
嗯,我的意思是,你可以獲得...
$array = [ [ 'name' => 'stuff I need' ] ];
導航到 $json_decode() 數組。
如果您問如何,那么,只需使用下一步......
$contentArray = $decoded[1] //With this you can get the array that you need.
- 1 回答
- 0 關注
- 119 瀏覽
添加回答
舉報
0/150
提交
取消