How does PHP filter arrays with Array_filter?

Source: Internet
Author: User
The database output array is

Array(    [0] => Array        (            [id] => 1            [name] => 你好,234            [cate] =>  生活日记       )    [1] => Array        (            [id] => 2            [name] => 79798            [cate] => 摄影美图        )    [2] => Array        (            [id] => 3            [name] => 567567            [cate] =>  生活日记       ))

Filter inside Cate = Photographic beauty, all other reservations, the effect is

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

How to implement it with Array_filter? Feel array_filter not very useful.

Reply content:

The database output array is

Array(    [0] => Array        (            [id] => 1            [name] => 你好,234            [cate] =>  生活日记       )    [1] => Array        (            [id] => 2            [name] => 79798            [cate] => 摄影美图        )    [2] => Array        (            [id] => 3            [name] => 567567            [cate] =>  生活日记       ))

Filter inside Cate = Photographic beauty, all other reservations, the effect is

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

How to implement it with Array_filter? Feel array_filter not very useful.

Do not say anything, directly on the code, unfortunately, PHP's closure and JS like the same smelly and long:

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

Note array_filter The second argument, the second parameter is a custom function. This custom function is intended to set the filter criteria.


  
   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);

I don't think it's possible to do it without array_filter.


  
    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);
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.