How does PHP use array_filter to Filter Arrays?

Source: Internet
Author: User
The database output array is {code...}, which filters out cate photos. All others are retained. The effect is {code...}. How can I use array_filter to implement this? It seems that array_filter is not easy to use. The database output array is

Array ([0] => Array ([id] => 1 [name] => Hi, 234 [cate] => life diary) [1] => Array ([id] => 2 [name] => 79798 [cate] => photo photos) [2] => Array ([id] => 3 [name] => 567567 [cate] => life diary ))

In the filter, cate = photographic meitu. All others are retained. The effect is

Array ([0] => Array ([id] => 1 [name] => Hi, 234 [cate] => life diary) [1] => Array ([id] => 2 [name] => 79798 [cate] => life diary ))

How to Use array_filter? It seems that array_filter is not easy to use.

Reply content:

The database output array is

Array ([0] => Array ([id] => 1 [name] => Hi, 234 [cate] => life diary) [1] => Array ([id] => 2 [name] => 79798 [cate] => photo photos) [2] => Array ([id] => 3 [name] => 567567 [cate] => life diary ))

In the filter, cate = photographic meitu. All others are retained. The effect is

Array ([0] => Array ([id] => 1 [name] => Hi, 234 [cate] => life diary) [1] => Array ([id] => 2 [name] => 79798 [cate] => life diary ))

How to Use array_filter? It seems that array_filter is not easy to use.

Let's not talk about it, but directly code it. Unfortunately, the closure of php is as smelly and long as javascript:

$ Data = [['id' => 1, 'name' => 'hello, 000000', 'cate' => 'life diaries '], ['id' => 2, 'name' => '000000', 'cate' => 'photo meitu '], ['id' => 3, 'name' => '20140901', 'cate' => 'life diaries '],]; $ filtered = array_filter ($ data, function ($ item) {return $ item ['cate']! = 'Photo meitu ';}); print_r ($ filtered );

Note:array_filterThe second parameter. The second parameter is a user-defined function. This custom function is used to set filtering conditions.


  1, 'cate' => "Life Diary"), array ('id' => 2, 'cate' => "photo"),); var_dump ($ arr ); function filter ($ rows) {if ($ rows ['cate'] = "") {return false;} else {return true ;}} $ arr = array_filter ($ arr, 'filter'); var_dump ($ arr );

I think array_filter can be used to achieve the goal.


  Array ('id' => '1', 'name' => 'hello, 100', 'cate' => 'life diaries '), 1 => array ('id' => '2', 'name' => '000000', 'cate' => 'photo meitu '), 2 => array ('id' => '3', 'name' => '000000', 'cate' => 'life diaries '),); $ resultArr = array (); foreach ($ oriArr as $ key => $ value) {if ($ value ['cate'] = 'photo meitu ') {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.