printing - 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?
Answer
Solution:
As @u_mulder said, you should store the result in another array.
You can also use , and avoid the
unset()
call.
$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>";
Source