Page 1 of 1

array_map not working

Posted: Wed Aug 22, 2007 11:46 am
by nwp
If string indexed array is given to array_map it returns numerically indexed array after applying the callback.See this code

Code: Select all

<?php
$r = array('one' => 'HeLlO', 'two' => 'Hi');
print_r($r);//Orgiginal array
$r = array_map('tst_it', $r, array(false));
function tst_it($r, $bool = false){
	if($bool){return strtoupper($r);}
	else{return strtolower($r);}
}
print_r($r);//Array after array_map
?>
But this is giving
Array
(
[one] => HeLlO
[two] => Hi
)
Array
(
[0] => hello
[1] => hi
)
See I've marked it bold.
Its numarically indexed instead of string indexed.
But how can I make it string index with the same key as original ??

Posted: Wed Aug 22, 2007 11:48 am
by feyd

Posted: Wed Aug 22, 2007 11:54 am
by nwp
What type of answer is this ??

Posted: Wed Aug 22, 2007 12:03 pm
by feyd
nwp wrote:What type of answer is this ??
A list of functions that may be of interest.

Posted: Wed Aug 22, 2007 12:09 pm
by nwp
But I wanna know why its giving such answer and how can I make it as I told above.

Posted: Wed Aug 22, 2007 12:16 pm
by feyd
Your code was not engineered for array_map(). It was built for array_walk().

Posted: Wed Aug 22, 2007 1:35 pm
by nwp
Aha! Thanks for the tip.