Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:49090 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 51693 invoked from network); 16 Jul 2010 09:52:53 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jul 2010 09:52:53 -0000 Authentication-Results: pb1.pair.com header.from=rquadling@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=rquadling@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.170 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: rquadling@gmail.com X-Host-Fingerprint: 74.125.82.170 mail-wy0-f170.google.com Received: from [74.125.82.170] ([74.125.82.170:45600] helo=mail-wy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F5/76-12736-3FB204C4 for ; Fri, 16 Jul 2010 05:52:51 -0400 Received: by wyb34 with SMTP id 34so1637857wyb.29 for ; Fri, 16 Jul 2010 02:52:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:received:reply-to :in-reply-to:references:from:date:message-id:subject:to:cc :content-type:content-transfer-encoding; bh=hAswrRdl58BCEKB3CrRorARqc4ofEShq9mtWjuET+j4=; b=PEebsqrPjPRb93KzyHnoTSnrpBlkzAyAM+bnqOPLn0ZqHIT+1dRQFIgD9VvNxzzVh2 V55XaVtMJe9NvXef8hItu2LTRFFcF/aEGqYrcT9RTvsjWyZVD45VuougSLJWDQsdI/vu enQSCRQg5iw5R4J1hXMWYSRuYKljM76/G4L3E= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:reply-to:in-reply-to:references:from:date:message-id :subject:to:cc:content-type:content-transfer-encoding; b=It8pglU1Sg+JpzNgj+k9IWLn4iN1Jdlf5nP5/Uz1NVr3PModdHShNUgMq7Yj0p1u18 +SATczHEfbWuLJgC25klwsM/LNu+0I/z004Im7MKxVmrbgFT9WchnoDBr6qvfx+Zswg9 IFJxB/gsjZR0Bf26BXrOaYXE3BCxakD5eK05s= Received: by 10.216.167.195 with SMTP id i45mr607305wel.34.1279273968186; Fri, 16 Jul 2010 02:52:48 -0700 (PDT) MIME-Version: 1.0 Received: by 10.216.17.148 with HTTP; Fri, 16 Jul 2010 02:52:28 -0700 (PDT) Reply-To: RQuadling@googlemail.com In-Reply-To: References: Date: Fri, 16 Jul 2010 10:52:28 +0100 Message-ID: To: Joel Perras Cc: internals Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Unintuitive array_combine() behaviour From: rquadling@gmail.com (Richard Quadling) On 16 July 2010 02:42, Joel Perras wrote: > array_combine(array(), array()) triggers an E_WARNING and returns > false, instead of simply returning an empty array with no triggered > warning. > > This is not a bug, but was intentionally written as such - see > ext/standard/array.c around lines 4480-4483 in the 5.3 branch in the > current svn HEAD: > > > PHP_FUNCTION(array_combine) > { > =C2=A0 =C2=A0 =C2=A0 =C2=A0zval *values, *keys; > =C2=A0 =C2=A0 =C2=A0 =C2=A0HashPosition pos_values, pos_keys; > =C2=A0 =C2=A0 =C2=A0 =C2=A0zval **entry_keys, **entry_values; > =C2=A0 =C2=A0 =C2=A0 =C2=A0int num_keys, num_values; > > =C2=A0 =C2=A0 =C2=A0 =C2=A0if (zend_parse_parameters(ZEND_NUM_ARGS() TSRM= LS_CC, "aa", &keys, > &values) =3D=3D FAILURE) { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return; > =C2=A0 =C2=A0 =C2=A0 =C2=A0} > > =C2=A0 =C2=A0 =C2=A0 =C2=A0num_keys =3D zend_hash_num_elements(Z_ARRVAL_P= (keys)); > =C2=A0 =C2=A0 =C2=A0 =C2=A0num_values =3D zend_hash_num_elements(Z_ARRVAL= _P(values)); > > =C2=A0 =C2=A0 =C2=A0 =C2=A0if (num_keys !=3D num_values) { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0php_error_docref(N= ULL TSRMLS_CC, E_WARNING, "Both parameters should > have an equal number of elements"); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0RETURN_FALSE; > =C2=A0 =C2=A0 =C2=A0 =C2=A0} > > =C2=A0 =C2=A0 =C2=A0 =C2=A0if (!num_keys) { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0php_error_docref(N= ULL TSRMLS_CC, E_WARNING, "Both parameters should > have at least 1 element"); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0RETURN_FALSE; > =C2=A0 =C2=A0 =C2=A0 =C2=A0} > > // [snip] > > > There is currently one open ticket regarding this behaviour > (http://bugs.php.net/bug.php?id=3D34857) from 2005, as well as a ticket > that was closed, stating that the incorrect E_WARNING message was > fixed (http://bugs.php.net/bug.php?id=3D29972). This behaviour is also > documented on http://php.net/array_combine . > > So, it's easy to see that this has been around for a while. However, I > don't think this behaviour is intuitive or very logical. Am I missing > something obvious? > > Thanks, > Jo=C3=ABl. > > P.S. > I've searched back to about 2004 in the php-internals list to see if > this topic has come up before, but came up empty. If this has already > been discussed ad nauseam, could someone please link me to the > relevant posts? > > -- > I do know everything, just not all at once. It's a virtual memory problem= . > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > [1] contains the initial submission of this function. It was commited on Jan 13 18:12:23 2003 UTC (7 years, 6 months ago) by andr= ey "added array_combine(). Creates an array by using the elements of the first parameter as keys and the elements of the second as correspoding keys. Error is thrown in case the arrays has different number of elements. Number of elements 0 is not valid for both parameters." As a lurker here, generating an error does seem odd. It forces addition userland work for seemingly no benefit. So a +1 for array() =3D=3D=3D array_combine(array(), array()) Regards, Richard. [1] http://svn.php.net/viewvc?view=3Drevision&revision=3D111688