php - php - 如何过滤掉值并将它们打印到另一个数组中
问题描述
如何从数组中过滤出我的值并稍后打印它们?过滤可以通过以下方式完成:
$array = explode("<br>", $list);
foreach( $array as $key => $value){
if (
strpos(strtolower($value),'item to be filtered') !== FALSE ||
strpos(strtolower($value),'another item to be filtered') !== FALSE
) {
unset($array[$key]);
}
};
$newcontent = "<pre>".implode("\n",$array)."</pre>";
但是我怎样才能在其他地方打印过滤后的数据呢?
解决方案
正如@u_mulder 所说,您应该将结果存储在另一个数组中。
您也可以使用array_filter()
, 并避免unset()
调用。
$list = "item to BE filtered<br>test<br>test<br>text another item to BE filtered";
$array = explode("<br>", $list);
$other = array_filter($array, function($value) {
return stripos($value,'item to be filtered') === FALSE &&
stripos($value,'another item to be filtered') === FALSE;
});
$newcontent = "<pre>".implode("\n", $other)."</pre>";
推荐阅读
- elfinder - 使用复选框 el finder 选择多个文件
- sql - Postgres 索引性能问题
- python - 在 confluent_kafka v0.11.2 for Python 中恰好有一次
- sql-server - 在包含日期的 SQL 表中使用 txt 文件批量插入返回 Msg 7339 列日期的无效数据
- php - 如何根据 PHP 上的多个 URL 查询值过滤内容
- jquery - 如何制作音频播放器的条形图
- ios - 向 CoreData 添加新版本
- sql - SQL:使用两个查询的结果计算 %
- c# - C# .Net 浏览器点击 Home/Index 而不被发送到那里
- ios - iOS 推送通知取决于站点内容