Someone else helped me write an array filter function that can pass parameters. how can I use it?

Source: Internet
Author: User
This is to delete an array smaller than 123. I did the experiment and the result was not good. how can I change it? Elements of the array that will not be deleted while learning and researching... {code...}. this is to delete elements smaller than 123 in the array,
I'm not doing this experiment. how can I change it?

Elements of arrays that are not deleted while learning and researching ..

$ Arr = Array (1, 2, 3713231987,3710001690, 371318yb3714441990, 3713231932); user_array_filter ($ arr, 'myg', 123); echo'
';print_r($arr);echo '
'; Function user_array_filter (& $ array, $ callback) {$ args = func_get_args (); // save all function parameters as an array; if (count ($ args) <2) exit ('function user_array_filter () Parameter error. there are at least two parameters. '); if (count ($ args) = 2) {foreach ($ array as $ k => $ v) {$ result = $ callback ($ v ); if (! $ Result) {return false ;//★★★★★★★★★★★★★★★★★★★★★★}}} Else {$ limit = $ args [2]; // [0]-> process an array, [1]-> function, [2]-> value foreach ($ array as $ k => $ v) {$ result = $ callback ($ v, $ limit); if (! $ Result) {return false ;//★★★★★★★★★★★★★★★★★★★★★★}}} Function myGt ($ val, $ limit = 88) {if ($ val >=$ limit) return true; return false ;}

Reply content:

This is to delete less than 123 of the array,
I'm not doing this experiment. how can I change it?

Elements of arrays that are not deleted while learning and researching ..

$ Arr = Array (1, 2, 3713231987,3710001690, 371318yb3714441990, 3713231932); user_array_filter ($ arr, 'myg', 123); echo'
';print_r($arr);echo '
'; Function user_array_filter (& $ array, $ callback) {$ args = func_get_args (); // save all function parameters as an array; if (count ($ args) <2) exit ('function user_array_filter () Parameter error. there are at least two parameters. '); if (count ($ args) = 2) {foreach ($ array as $ k => $ v) {$ result = $ callback ($ v ); if (! $ Result) {return false ;//★★★★★★★★★★★★★★★★★★★★★★}}} Else {$ limit = $ args [2]; // [0]-> process an array, [1]-> function, [2]-> value foreach ($ array as $ k => $ v) {$ result = $ callback ($ v, $ limit); if (! $ Result) {return false ;//★★★★★★★★★★★★★★★★★★★★★★}}} Function myGt ($ val, $ limit = 88) {if ($ val >=$ limit) return true; return false ;}

Change the else in user_array_filter to the following code:

Else {$ limit = $ args [2]; // [0]-> process arrays, [1]-> function, [2]-> value foreach ($ array as $ k => $ v) {$ result = $ callback ($ v, $ limit); if (! $ Result) {unset ($ array [$ k]) ;}}

Ps: If you want to delete array elements, you can foreach the array and then drop the entire key unset () if the conditions are not met in the loop.

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.