Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60687 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 9044 invoked from network); 29 May 2012 08:05:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 May 2012 08:05:15 -0000 Authentication-Results: pb1.pair.com smtp.mail=glopes@nebm.ist.utl.pt; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=glopes@nebm.ist.utl.pt; sender-id=unknown Received-SPF: error (pb1.pair.com: domain nebm.ist.utl.pt from 193.136.128.21 cause and error) X-PHP-List-Original-Sender: glopes@nebm.ist.utl.pt X-Host-Fingerprint: 193.136.128.21 smtp1.ist.utl.pt Linux 2.6 Received: from [193.136.128.21] ([193.136.128.21:34923] helo=smtp1.ist.utl.pt) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A1/65-13113-73384CF4 for ; Tue, 29 May 2012 04:05:12 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp1.ist.utl.pt (Postfix) with ESMTP id 8B8357000348; Tue, 29 May 2012 09:05:08 +0100 (WEST) X-Virus-Scanned: by amavisd-new-2.6.4 (20090625) (Debian) at ist.utl.pt Received: from smtp1.ist.utl.pt ([127.0.0.1]) by localhost (smtp1.ist.utl.pt [127.0.0.1]) (amavisd-new, port 10025) with LMTP id t6jhCZLQ2rag; Tue, 29 May 2012 09:05:08 +0100 (WEST) Received: from mail2.ist.utl.pt (mail.ist.utl.pt [IPv6:2001:690:2100:1::8]) by smtp1.ist.utl.pt (Postfix) with ESMTP id 1BF597000347; Tue, 29 May 2012 09:05:07 +0100 (WEST) Received: from slws007.slhq.int (a82-161-209-109.adsl.xs4all.nl [82.161.209.109]) (Authenticated sender: ist155741) by mail2.ist.utl.pt (Postfix) with ESMTPSA id 7B3D8200BB75; Tue, 29 May 2012 09:05:07 +0100 (WEST) Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes To: "Stas Malyshev" Cc: "internals@lists.php.net" References: <4FC41815.3060306@sugarcrm.com> Date: Tue, 29 May 2012 10:05:06 +0200 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Organization: =?utf-8?Q?N=C3=BAcleo_de_Eng=2E_Biom=C3=A9di?= =?utf-8?Q?ca_do_I=2ES=2ET=2E?= Message-ID: In-Reply-To: <4FC41815.3060306@sugarcrm.com> User-Agent: Opera Mail/11.64 (Win32) Subject: Re: [PHP-DEV] [VOTE] array_part() From: glopes@nebm.ist.utl.pt ("Gustavo Lopes") On Tue, 29 May 2012 02:28:05 +0200, Stas Malyshev wrote: > This looks very complicated. It might be that some use cases require > this level of complexity, but I can't remember a case where I would need > it. That alone does not disqualify the proposal, but I personally very > much like having something like array_column() rather than whole array > slicing engine with it's own declarative language inside. The engine > might be fine, but I imagine many users would want just something like > array_column() - so at least it'd be nice to have both. 1) Independently for whether we want array_column this function does things that no other function built-in currently does and some that no userland function can even do: * Extract slices without side effects (array_slice() has side effects). * Extract slices based on keys on time proportional to the size of the slice and not to the size of the array. * Pick an arbitrary number of elements from an array in any permutation and traversing the array, in worst case -- if we request an element in the middle --, only once. 2) A lot of people seem to think this is too complicated. I think that objection comes from several places: * People focusing on the most complex usages of the function. The test cases have to cover complex usages in order to exercise the implementation correctly, but that doesn't mean the majority of use cases will be that complex. * The syntax for contiguous slices is verbose (e.g. ['start'=>1, 'end'=>3] instead of [1:3] or [1..3] like in other languages). * Specifying we want all the elements in a level could be simpler (e.g. using null instead of ['start'=>0] or ['start'=>null]). See above. * The description of the function aims to be precise and is therefore verbose. This was probably a strategic miscalculation on my part. Let me exemplify how this function may make things simpler. Right now, let's suppose you want to drop the first element on each level. Let's say you have this array: $arr = [ ['col1', 'col2', 'col3'], ['line1', 0, 1 ], ['line2', 1, 0 ], ] To drop the headers, you'd now have to do: array_slice(array_map(function($a) { return array_slice($a,1); }, $arr), 1); With array_part, you can do: array_part($arr, [ ['start'=>1], ['start'=>1] ]); or in this case: array_part($arr, [ [1, 2], [1, 2] ]); -- Gustavo Lopes