我正在嘗試過濾并刪除與特定值匹配的數組。我從 API 獲取 JSON,然后使用 PHP json_decode 對其進行解碼。它顯示得很好,但它給出了我不想要的值。JSON 文件 = https://pastebin.com/raw/7yW1CEdu我正在使用以下 foreach 語句,該語句可以工作并顯示我需要的每個統計數據的數據(為了專注于刪除數組,我已將其剝離):<?php foreach($json['response']['data'] as $item) { $newarray = array_filter($item['competitionName'], function($var) { return ($var != 'Junior SS Premiership Zone 3'); }); }?>這就是我希望它與當前外觀相比的樣子 - https://gyazo.com/d8654cc939dba9e0e52f06e66f489323我的 array_filter 代碼有什么問題?我希望它刪除任何明確聲明的數組: "competitionName":"Junior SS Premiership Zone 3"因此該數組中的任何數據都不會在 foreach 中處理。謝謝!
1 回答

哈士奇WWW
TA貢獻1799條經驗 獲得超6個贊
$item['competitionName']是一個字符串,而不是字符串數組。我想你想要的是:
$data = array_filter($json['response']['data'], function ($item) {
return $item['competitionName'] != 'Junior SS Premiership Zone 3';
});
foreach ($data as $item) {
// display the data
}
或者不要理會過濾器,只需在主循環中檢查它并跳過它。
foreach ($json['response']['data'] as $item) {
if ($item['competitionName'] == 'Junior SS Premiership Zone 3') {
continue;
}
// process the item
}
- 1 回答
- 0 關注
- 110 瀏覽
添加回答
舉報
0/150
提交
取消