Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60580 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 83884 invoked from network); 14 May 2012 14:03:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 May 2012 14:03:27 -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.22 cause and error) X-PHP-List-Original-Sender: glopes@nebm.ist.utl.pt X-Host-Fingerprint: 193.136.128.22 smtp2.ist.utl.pt Linux 2.6 Received: from [193.136.128.22] ([193.136.128.22:50038] helo=smtp2.ist.utl.pt) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F5/07-44671-DA011BF4 for ; Mon, 14 May 2012 10:03:26 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp2.ist.utl.pt (Postfix) with ESMTP id 8B0347000441; Mon, 14 May 2012 15:03:22 +0100 (WEST) X-Virus-Scanned: by amavisd-new-2.6.4 (20090625) (Debian) at ist.utl.pt Received: from smtp2.ist.utl.pt ([127.0.0.1]) by localhost (smtp2.ist.utl.pt [127.0.0.1]) (amavisd-new, port 10025) with LMTP id fEtKgS5BIaFB; Mon, 14 May 2012 15:03:22 +0100 (WEST) Received: from mail2.ist.utl.pt (mail.ist.utl.pt [IPv6:2001:690:2100:1::8]) by smtp2.ist.utl.pt (Postfix) with ESMTP id 1A916700043D; Mon, 14 May 2012 15:03:22 +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 AF121203BBF5; Mon, 14 May 2012 15:03:21 +0100 (WEST) Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes To: "Pierre Joye" , "Paul Dragoonis" Cc: "internals@lists.php.net" References: Date: Mon, 14 May 2012 16:03:21 +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: User-Agent: Opera Mail/11.62 (Win32) Subject: Re: [PHP-DEV] [RFC] [DISCUSSION] array_part() From: glopes@nebm.ist.utl.pt ("Gustavo Lopes") On Mon, 14 May 2012 15:41:25 +0200, Paul Dragoonis wrote: > Gustavo, why would I use array_part() if I could use array_slice() to get > the parts of an array between two offsets. > Obviously, if you want to do something that array_slice() already does, then you wouldn't have a lot of pressing reasons to use this instead. But this is not a very interesting question. On the other hand, if you're asking what are differences between this and array_slice(), then: * array_slice() only does slicing at the level 1 (it has no multidimensional slicing that would allow doing something like array_column(), see https://github.com/php/php-src/pull/56 ) * array_slice() only operates in an 'index-as-offset' mode, which is less efficient than 'index-as-key'. array_part() allows you to choose: $arr = [-1 => 'a', 0 => 'b']; array_slice($arr, 0, 1) // 'a' array_slice($arr, -1, 1) // 'b' array_part($arr, [0]) // 'a' array_part($arr, [-1]) // 'b' array_part($arr, [-1], true) // 'a' * array_slice() has side-effects; it resets the internal array pointer. * array_slice() can preserve keys, array_part() cannot because when collapsing levels this would mean the possibility of overwriting elements. I'll improve the RFC with inline examples and this information later. -- Gustavo Lopes