Discussion:
array_map() with multiple callback functions
George Langley
2013-05-07 21:29:16 UTC
Permalink
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,
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Alex Nikitin
2013-05-07 21:43:25 UTC
Permalink
Something like:

$cleanData = array_map(function($str){return strtolower(trim($str));},
$passedData);
--
The trouble with programmers is that you can never tell what a
programmer is doing until it’s too late. ~Seymour Cray
Post by George Langley
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.
// 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,
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Andrew Ballard
2013-05-09 00:26:44 UTC
Permalink
Post by Alex Nikitin
Post by George Langley
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.
// 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,
$cleanData = array_map(function($str){return strtolower(trim($str));},
$passedData);
I'd go with this general approach, whether you use a named function or
an anonymous function for the callback. I don't know how large the
array is, but option #1 iterates the input array twice and option #2
iterates the array three times. If you eventually need to add
additional functions to clean the input, they would add even more
iterations. This approach only iterates once.

Andrew
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2013-05-08 19:48:56 UTC
Permalink
Post by George Langley
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.
// 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);
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
George Langley
2013-05-09 05:08:04 UTC
Permalink
Post by George Langley
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.
// 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);
}
--
$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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Search results for 'array_map() with multiple callback functions' (Questions and Answers)
6
replies
help needed in c program?
started 2007-04-20 06:45:26 UTC
programming & design
Loading...