Algorithm, which is difficult for conditional running of custom functions. Thanks

Source: Internet
Author: User
It is difficult to find an algorithm for conditional execution of user-defined functions. Thank you! In PHP, you need to find an algorithm. for example, $ arr0, $ arr1, and $ arr2, $ arr3, the number of elements is fixed by 4, $ arr0 = array ('', 'wc1','', 'wc3'); // This array is defined, it is difficult to evaluate an algorithm for executing those functions and methods $. Thank you!
PHP code
  In PHP, please find an algorithm. for example: $ arr0, $ arr1 and $ arr2, $ arr3, the number of elements is fixed 4, $ arr0 = array ('', 'wc1','', 'wc3'); // This array is defined, to execute those functions and methods $ arr1 = array ('wc0 ', 'wc1', 'wc2', ''); $ arr2 = array ('wc0 ','', '', 'wc3'); a total of four custom functions, namely function wc0 () {} function wc1 () {} function wc2 () {} function wc3 () {} problem start: For arrays $ arr0, $ arr1, and $ arr2, when the element is not empty, the corresponding user-defined function is executed. For example: when $ arr0 = array ('', 'wc1','', 'wc3') is encountered, two functions are executed: wc1 () and wc3 () when $ arr1 = array ('wc0 ', 'wc1', 'wc2 ', '') is encountered, three functions are executed: wc0 (), wc1 (), wc2 () when $ arr2 = array ('wc0 ', '','', 'wc3') is encountered, two functions are executed, wc0 (), wc3 ()....... do you see the rule? That's exactly what it looks like. now I want to rewrite it into a function and find an algorithm. That is, when $ arr is encountered, XXXX is executed.


------ Solution --------------------
PHP code
Function wc0 () {echo "wc0 \ n";} function wc1 () {echo "wc1 \ n";} function wc2 () {echo "wc2 \ n ";} function wc3 () {echo "wc3 \ n" ;}$ arr = array ('', 'wc1','', 'wc3 '); foreach ($ arr as $ func) {if (function_exists ($ func) {$ func ();}}
------ Solution --------------------
Either array_product or array_reduce can be used.

For
$ Ar = array ('', 'wc1','', 'wc3 ');
$ R = array ();
Foreach ($ ar as $ func)
$ R [] = $ func? $ Func (): 1;
Echo array_product ($ r );

& Equal to multiplication
Assume that wc1 returns 0 and wc3 returns 1.
Then, the value of $ r is 1, 0, and the value is 0 after the multiplication of 1, 1, and 1.

Assume that wc1 returns 1 and wc3 returns 1.
Then, $ r is, and 1 is obtained after multiplication.

------ Solution --------------------
PHP code
    ------ Solution --------------------
This is relatively simple.
PHP code
$a=Array('a','b','','c');For($i=0;$i<4;$i++) {    IF($a[$i]!='') {        eval("wc{$i}();");    }}function wc0(){    Echo '0';}function wc1(){    Echo '1';}function wc2(){    Echo '2';}function wc3(){    Echo '3';}

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.