In PHP, array_walk()
and array_map()
two functions can be implemented to modify each value in the array, for example, this example is to convert all the values in the arrays from UTF-8 encoding to GBK encoding.
Of course, in addition to these two functions, can also be used to foreach
iterate the array implementation, but compared to the direct use of PHP's internal functions, obviously less efficient and not elegant.
array_wark()
Here's how it's implemented:
ARRAY2GBK($array) { function(& $value) {$value = Iconv (return $array;}
array_map()
Here's how it's implemented:
ARRAY2GBK($array) { $array = Array_map (functionreturn iconv (return $array;}
It is also possible to see the similarities and differences between the two functions:
- The $value that pass through the two functions is the single element in the array.
array_walk()
Returns only true
or false
array_map()
returns the processed array;
- To get the value of the processed element, you need to return it if you want to
array_walk()
add a reference symbol to the parameter value passed in &
array_map()
return
.
Both of these methods can achieve the same function, choose one.
PHP Array_walk and ARRAY_MAP functions implement array values UTF-8 to GBK encoding