Home >Backend Development >PHP Tutorial >PHP如何用array_filter过滤数组?

PHP如何用array_filter过滤数组?

WBOY
WBOYOriginal
2016-06-06 20:26:541928browse

数据库输出数组是

<code>Array
(
    [0] => Array
        (
            [id] => 1
            [name] => 你好,234
            [cate] =>  生活日记
       )

    [1] => Array
        (
            [id] => 2
            [name] => 79798
            [cate] => 摄影美图

        )

    [2] => Array
        (
            [id] => 3
            [name] => 567567
            [cate] =>  生活日记
       )

)</code>

过滤里面cate = 摄影美图,其它全部保留,效果是

<code>Array
(
   [0] => Array
       (
           [id] => 1
           [name] => 你好,234
           [cate] =>  生活日记
       )

   [1] => Array
       (
           [id] => 2
           [name] => 79798
           [cate] => 生活日记

      )
)</code>

如何用array_filter实现呢? 感觉array_filter不太好用。

回复内容:

数据库输出数组是

<code>Array
(
    [0] => Array
        (
            [id] => 1
            [name] => 你好,234
            [cate] =>  生活日记
       )

    [1] => Array
        (
            [id] => 2
            [name] => 79798
            [cate] => 摄影美图

        )

    [2] => Array
        (
            [id] => 3
            [name] => 567567
            [cate] =>  生活日记
       )

)</code>

过滤里面cate = 摄影美图,其它全部保留,效果是

<code>Array
(
   [0] => Array
       (
           [id] => 1
           [name] => 你好,234
           [cate] =>  生活日记
       )

   [1] => Array
       (
           [id] => 2
           [name] => 79798
           [cate] => 生活日记

      )
)</code>

如何用array_filter实现呢? 感觉array_filter不太好用。

啥也不说,直接上代码,可惜php的闭包跟js一样又臭又长:

<code>$data = [
   [ 'id' => 1, 'name' => '你好,234', 'cate' => '生活日记'],
   [ 'id' => 2, 'name' => '79798', 'cate' => '摄影美图'],
   [ 'id' => 3, 'name' => '567567', 'cate' => '生活日记'],
];

$filtered = array_filter($data, function($item){ 
                 return $item['cate'] !== '摄影美图'; 
            });
print_r($filtered);</code>

注意看array_filter的第二个参数,第二个参数是一个自定义函数。这个自定义函数是为了设置过滤条件。

<code><?php $arr = array(
        array('id'=>1, 'cate'=>"生活日记"),
        array('id'=>2, 'cate'=>"摄影美图"),
    );
    var_dump($arr);
    
    function filter($rows){
        if($rows['cate']==="摄影美图"){
            return false;
        }else{
            return true;
        }
    }
    
    $arr = array_filter($arr, 'filter');
    var_dump($arr);

</code>

我觉得不用array_filter也能达到目的呀

<code><?php $oriArr = array(
       0 => array('id' => '1', 'name' => '你好,234', 'cate' => '生活日记'),
       1 => array('id' => '2', 'name' => '79798', 'cate' => '摄影美图'),
       2 => array('id' => '3', 'name' => '567567', 'cate' => '生活日记'),
   );

   $resultArr = array();
   foreach ($oriArr as $key => $value)
   {
       if ($value['cate'] === '摄影美图')
       {
           continue;
       }
       else
       {
           $resultArr[] = $value;
       }
   }
   print_r($resultArr);</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn