PHP Array_walk and ARRAY_MAP functions implement array values UTF-8 to GBK encoding

Source: Internet
Author: User

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:

    1. The $value that pass through the two functions is the single element in the array.
    2. array_walk()Returns only true or false array_map() returns the processed array;
    3. 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

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.