问题描述
我正在尝试过滤和删除与特定值匹配的数组。我从API获取JSON,然后通过PHP usng json_decode将其解码。它显示得很好,但带有我不想要的值。
JSON文件= https://pastebin.com/raw/7yW1CEdu
我正在使用下面的foreach语句,该语句可以工作并显示我需要的每个stat的数据(我剥离了它是为了专注于删除数组)
<?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中进行处理。
谢谢!
解决方法
$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
}