Re: Proposal for new array_map function to pass in keys

From: Date: Sat, 08 Jun 2013 07:13:51 +0000
Subject: Re: Proposal for new array_map function to pass in keys
References: 1  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
> 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)

« previous php.internals (#67653) next »