php - 从 foreach 中删除整个数组
问题描述
我正在尝试过滤和删除与特定值匹配的数组。我从 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 中处理。
谢谢!
解决方案
$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
}
推荐阅读
- xamarin.forms - xamarin forms:单击选项卡时如何关闭选项卡并显示上一页?
- r - 在ggplot r中创建带有因子变量和连续变量的折线图
- c# - 为 Microsoft echo bot 创建内存测试服务器
- ios - 多上下文核心数据:从 childContext 中删除对象
- postgresql - 在 JPA 中使用 Flyway 创建的序列
- ios - Apollo iOS swift 总是从本地缓存中获取 GraphQl
- .net-core - 在 Autofac 中注册回调并在回调中再次构建容器
- asp.net - 为什么无论是否选中复选框,剑道验证都不会让我通过
- c# - 如何解决在IIS中调试的问题
- mysql - MySQL 查询的等效 MemSQL 查询