简体   繁体   English

php - 如何过滤掉值并将它们打印在另一个数组中

[英]php - How to filter out values and print them in another array

How can I filter out my values out of an array and print them later?如何从数组中过滤掉我的值并稍后打印它们? The filtering can be done with:过滤可以通过以下方式完成:

$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>";

but how can I print the filtered data else where?但是如何在其他地方打印过滤后的数据?

As @u_mulder said, you should store the result in another array.正如@u_mulder 所说,您应该将结果存储在另一个数组中。

You can also use array_filter() , and avoid the unset() call.您还可以使用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>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM