Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67655 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 5087 invoked from network); 8 Jun 2013 08:57:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Jun 2013 08:57:45 -0000 Authentication-Results: pb1.pair.com header.from=theanomaly.is@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=theanomaly.is@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.83.43 as permitted sender) X-PHP-List-Original-Sender: theanomaly.is@gmail.com X-Host-Fingerprint: 74.125.83.43 mail-ee0-f43.google.com Received: from [74.125.83.43] ([74.125.83.43:45127] helo=mail-ee0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 65/12-25405-702F2B15 for ; Sat, 08 Jun 2013 04:57:44 -0400 Received: by mail-ee0-f43.google.com with SMTP id l10so1382000eei.2 for ; Sat, 08 Jun 2013 01:57:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=DRifUh3TgUao/xJYtv5F977NmVE27jcBlH4bD3J8CT8=; b=IQgCw8ld5+6qXaFgPdZGntPp+SrvUPjHhYYk1+lR1hbUqJBOz+HQ0DEbAX4BJzhxeq S0HX/lihKiMgCeiF99M/BRc0NIhxFhq9U8vI2BlF/dbywbFTtL+gjKLUxDpCHV+mrLu6 46FFuNWgCE1qLD99X32qjRuWsXQPqHVS+ts1MjOla0SG9yn8WT6GA0SOWCmmPdW97Sg4 zDB8yBdDihK0Qlx44WWMtv6ehK/HJHhp5niJbQxC1/Shyv+q8XKNjXmAfaUXD7VdHJCW wT3XRdPXYlYn7AG+6fO21JiUqK/cRsBJsg3A36fNNegY100RrTbOBnNZTNGIdiKBPB9P xmsA== MIME-Version: 1.0 X-Received: by 10.14.178.134 with SMTP id f6mr2310947eem.91.1370681861035; Sat, 08 Jun 2013 01:57:41 -0700 (PDT) Received: by 10.15.31.71 with HTTP; Sat, 8 Jun 2013 01:57:40 -0700 (PDT) In-Reply-To: References: Date: Sat, 8 Jun 2013 04:57:40 -0400 Message-ID: To: pierre@pcservice.co.za Cc: PHP Internals Content-Type: multipart/alternative; boundary=047d7b6242c06d7a1504dea0bd68 Subject: Re: [PHP-DEV] Proposal for new array_map function to pass in keys From: theanomaly.is@gmail.com (Sherif Ramadan) --047d7b6242c06d7a1504dea0bd68 Content-Type: text/plain; charset=ISO-8859-1 On Sat, Jun 8, 2013 at 3:13 AM, Pierre du Plessis wrote: > > 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. > My mistake, I left out the second argument to str_word_count, which should have been 1. In that case str_word_count would return an array of all the words in supplied string. > > 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? > The same thing that would happen if you did $array['PHP'] = 3; $array['PHP'] = 3; The value would be overridden. array_map_key would behave no differently than foreach would in this matter. The code would semantically equivalent to the following: function array_map_key(Callable $callback, $array) { $return = array(); foreach ($array as $key => $value) { list($k,$v) = $callback($key, $value); $return[$k] = $v; } return $return; } You can see the PHP user-land implementation here with your described example and the resulting output http://3v4l.org/pe855 --047d7b6242c06d7a1504dea0bd68--