Re: Proposal for new array_map function to pass in keys
> function my_call_back($key, $value) {
> return array($value, strlen($value));
> }
> $array = str_word_count("PHP is lots of fun!");
> $array = array_map_key('my_call_back', $array);
>
>
> The result would be the following array:
>
> array(5) {
> ["PHP"]=>
> int(3)
> ["is"]=>
> int(2)
> ["lots"]=>
> int(4)
> ["of"]=>
> int(2)
> ["fun"]=>
> int(3)
> }
>
>
This example doesn't make any sense, as str_word_count returns an integer,
so you would in fact pass an int to array_map_key and not an array.
But let's say using your example, you use explode(" ", $string) instead of
str_word_count, which will give you an array of all the words.
What happens in the following case:
function my_call_back($key, $value) {
return array($value, strlen($value));
}
$array = str_word_count("PHP stands for PHP hypertext preprocessor");
$array = array_map_key('my_call_back', $array);
This would give you 2 keys with the value of PHP. What happens in this case?
What if you have duplicate integer keys. Does the keys simply increment or
throw an error?
Thread (3 messages)