Re: Re: array_map() with multiple callback functions
| From: | George Langley | Date: | Thu, 09 May 2013 05:08:04 +0000 |
| Subject: | Re: Re: array_map() with multiple callback functions | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-321047@lists.php.net to get a copy of this message | ||
On 2013-05-08, at 1:48 PM, Jim Giner wrote:
> On 5/7/2013 5:29 PM, George Langley wrote:
>> Hi all. I want to apply strtolower() AND trim() to all items in an array. But I don't
>> see a way to call multiple callbacks with the array_map() function.
>> Are my two choices the following:
>>
>> // 1) nesting two array_map() calls
>> $cleanData = array_map('trim',(array_map('strtolower',$rawData)));
>>
>>
>> // 2) call my own function with array_walk()
>> $cleanData = array_walk('myCleaner',$rawData);
>>
>> function myCleaner($passedData){
>> $cleanData = array_map('strtolower',$passedData);
>> $cleanData = array_map('trim',$cleanData);
>> }
>> //(Of course, wouldn't bother with a function, just to call array_map twice...)
>>
>> Just seeing if there's a better way than having to go through the array twice to apply
>> each callback separately. Thanks,
>>
> Not sure if you have an answer to this yet, so I'll present my simpler approach.
>
> foreach ($my_array as &$v)
> {
> $v = trim($v);
> $v = strtolower($v);
> }
>
> --
On 2013-05-07, at 3:43 PM, Alex Nikitin wrote:
> Something like:
>
> $cleanData = array_map(function($str){return strtolower(trim($str));},
> $passedData);
------
Thanks guys - will check both out to see what works best in my situation.
George