Re: Passing arguments to an internal function via array_map
| From: | Simon J Welsh | Date: | Thu, 10 Nov 2011 17:59:46 +0000 |
| Subject: | Re: Passing arguments to an internal function via array_map | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-315629@lists.php.net to get a copy of this message | ||
On 11/11/2011, at 5:10 AM, Marc Guay wrote:
> Hi folks,
>
> I'm trying to convert the contents of an array from utf8 to utf16
> (thanks for the headache MS Excel!). I originally created a "user
> function" that just ran the text through
> mb_convert_encoding($text,'utf-16','utf-8') but now I'm wondering if I
> can't just use the internal function directly. Apparently it's more
> complicated when the function requires arguments; I see example using
> mysql_real_escape_string() on php.net.
>
> My current attempt looks like this:
>
> $clean = array_map('mb_convert_encoding', $dirty_arr,
> array('utf-16','utf-8'));
>
> but I believe this is actually passing an array to
> mb_convert_encoding() as the second argument rather than the 2nd and
> 3rd... Any ideas or am I chasing something that can't be done?
>
> Marc
You need to pass a second and third array to array_map() with the same number of elements as the
first array. The arguments to the callback function are the elements from each array at the same
offset.
Something like $clean = array_map('mb_convert_encoding', $dirty_arr, array_fill(0,
count($dirty_arr), 'utf-16'), array_fill(0, count($dirty_arr), 'utf-8'));
---
Simon Welsh
Admin of http://simon.geek.nz/