Share a function, find bugs, and improve it. ^_^ We often use many processing functions for numbers or strings in phper.
Unfortunately, most of them do not support arrays.
A flash, get it done
I think it's useful.
By the way, find some bugs and give some suggestions for improvement.
/*** Use recursive calls to allow functions to support multi-dimensional arrays by wandwind *. we recommend that you call other functions to implement * @ param string $ fn call function name * @ param array $ param parameter array, normally input func_get_args () * @ param int $ key the Recursive parameter key value * @ param boolean $ check whether the function validity needs to be checked * @ return mixed returns the running result of $ param [$ key */ function fn2array ($ fn, $ param, $ key = 0, $ check = true) {// The validity of the function is detected by default during external calls. if ($ check &&! Function_exists ($ fn) {return false;} // extract the execution target. if the target is not an array, run $ content = $ param [$ key]; if (! Is_array ($ content) {return call_user_func_array ($ fn, $ param);} // recursively execute foreach ($ content as & $ val) if the target is an array) {$ param [$ key] = $ val; $ val = fn2array ($ fn, $ param, $ key, false);} return $ content;} function wstripslashes () {return fn2array ('stripslashes', func_get_args ();} function wsubstr () {$ param = func_get_args (); $ param [3] = isset ($ param [3])? $ Param [3]: 'utf-8'; return fn2array ('MB _ substr ', $ param, 0 );} $ a = array (2 => '\ "aaaaaaaaaaaaaaa \"', array ('\ "ah \"'), array ('\ "sss \ dasf \ dasfasdsad \"'),); print_r (wstripslashes ($ a); print_r (wsubstr ($, 2, 5 ));
Reply to discussion (solution)
After posting a post, we think of an improvement direction. for example, we often filter a result set from the database, and we may need to filter several fields separately.
For example, stripslashes for title and content
The interface can be designed as wstripslashes ($ data, 'title, content ')
In this case, you need to determine the key value in one step.
You forgot the array_walk_recursive function of php.
You forgot the array_walk_recursive function of php.
There is indeed a bad habit of reading manuals.
But I just flipped through, and I thought this function could not meet the requirements.
1. how to support multi-dimensional arrays?
2. how can I use it to recursion functions such as iconv ('gbk', 'utf-8', $ str?
This function has never been used, so you may need to write an example.
$ Ar = array ('kanji '), array ('kanji '), array ('kanji '), array ('kanji'),); function zm (& $ v, $ k, $ charset) {$ v = iconv ($ charset [0], $ charset [1], $ v);} array_walk_recursive ($ ar, 'zm', array ('gbk ', 'utf-8'); print_r ($ ar );
?? Supported.
$ Ar = array ('kanji '), array ('kanji '), array ('kanji '), array ('kanji'),); function zm (& $ v, $ k, $ charset) {$ v = iconv ($ charset [0], $ charset [1], $ v);} array_walk_recursive ($ ar, 'zm', array ('gbk ', 'utf-8'); print_r ($ ar );
Thanks to xu Da for learning more.
Functions provided by the system may have better performance.
However, in combination with the development experience, I still want to support my own methods
Array_walk_recursive references the original data and the parameter passing method is too dead. if you want to avoid contamination of $ ar and make it easier for developers to use it, you need to define another function.
function zm(&$v, $k, $charset) { $v = iconv($charset[0], $charset[1], $v);}function wiconv($in, $out, $content) { array_walk_recursive($content, 'zm', array($in, $out)); return $content;}
In fact, this wiconv does not work either. adding $ content itself is the criterion for determining strings ~
I want to create a new function quickly without changing the usage habits of the original function. what kind of object-oriented inheritance idea does it look like?
For example, in my method, iconv can be extended as follows:
Function wiconv () {// 2 indicates recursion. The third parameter is the target return fn2array ('iconv', func_get_args (), 2);} $ ar = array ('kanji ', array ('kanji '); print_r (wiconv ('utf-8', 'gbk', $ ar); // do not change the original function usage habits
What do you think?
?? Supported.
function wiconv($in_charset, $out_charset, $str) { if(is_array($str)) { array_walk_recursive($str, function(&$v, $k, $p) { $v = iconv($p[0], $p[1], $v); }, array($in_charset, $out_charset)); return $str; } return iconv($in_charset, $out_charset, $str);}
I think of another famous saying: people who duplicate the wheel
I don't quite understand what the function means, but let's take a look.
I think of another famous saying: people who duplicate the wheel
If the wheel of our predecessors is not good, we will try to make a better one. even if it is really bad, this is the first step to make a better wheel ~ What do you mean
There is nothing wrong with wheel building. only by choosing materials and technologies can you get twice the result with half the effort.