我從 api 獲取 json 資料,我嘗試洗掉所有重復元素(如果 2 個元素被命名為“aa”,我想洗掉兩者之一)為此我這樣做
$plugins = Http::accept('application/json')->get('https://poggit.pmmp.io/plugins.min.json')->object();
$plugins = array_unique($plugins);
但我明白了
類 stdClass 的物件無法轉換為字串。
當我gettype在$plugins我得到array。
我嘗試過
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://poggit.pmmp.io/plugins.min.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Accept: */*'; // you can try to change with application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$plugins = json_decode($result, true); // decode to array
$plugins = array_unique($plugins);
但是在這里我在'$plugins = array_unique($plugins);'處進行了陣列到字串的轉換
為什么我有這個錯誤?
uj5u.com熱心網友回復:
所以我嘗試使用你的 url在 php 中GET發出請求cURL,這里是我的嘗試:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://poggit.pmmp.io/plugins.min.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Accept: */*'; // you can try to change with application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$plugins = json_decode($result, true); // decode to array
$plugins = array_unique($plugins); // make it unique
print_r($plugins); // It's work!
uj5u.com熱心網友回復:
使用SORT_REGULAR標志來比較元素,但請注意,只有當它們具有相同的鍵值對時,陣列才被認為是相等的:
$plugins = array_unique($plugins, SORT_REGULAR);
從檔案:
注意:兩個元素被認為相等當且僅當 (string) $elem1 === (string) $elem2 即當字串表示相同時,將使用第一個元素。
參考:https : //www.php.net/array_unique
uj5u.com熱心網友回復:
你有沒有嘗試轉換stdClass到以Array使用json_decode($plugins, true);使用前array_unique($plugins)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/407236.html
標籤:
上一篇:即使輸入存在,輸入驗證也會出錯
下一篇:我不懂PHP參考資料
