【问题标题】:php - How to filter out values and print them in another arrayphp - 如何过滤掉值并将它们打印到另一个数组中
【发布时间】:2021-02-18 05:36:09
【问题描述】:

如何从数组中过滤出我的值并稍后打印它们? 过滤可以通过:

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

但是如何在其他地方打印过滤后的数据?

【问题讨论】:

  • 将过滤后的值存储在另一个数组中。
  • 旁白:您可能可以使用stripos() 而不是strpos(strtolower())。
  • @u_mulder 请提供示例
  • $anotherArray[] = 'value';
  • 这只是打印单词值

标签: php printing filtering


【解决方案1】:

正如@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>";

【讨论】:

  • 这不会过滤我的数组
  • @MarkdeGraaff 抱歉,我打印的是“$array”而不是“$other”。见3v4l.org/2XIZp
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 2016-09-23
  • 2017-02-16
  • 2018-10-04
  • 1970-01-01
相关资源
最近更新 更多