Re: Unintuitive array_combine() behaviour
| From: | Joel Perras | Date: | Thu, 26 Aug 2010 17:07:12 +0000 |
| Subject: | Re: Unintuitive array_combine() behaviour | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-49545@lists.php.net to get a copy of this message | ||
Since there didn't seem to be anyone who disagreed on the odd
behaviour of array_combine() with empty arrays as arguments, I've
written up a patch for this and attached it to the existing feature
change/request report:
Report: http://bugs.php.net/bug.php?id=34857
Patch: http://paste.roguecoders.com/p/16e1e953e8b1890bcaaba00eb8408dd7.txt
If someone with the appropriate karma could review this and commit it
to trunk, that would be grand.
Thanks,
Joël.
On Fri, Jul 16, 2010 at 5:52 AM, Richard Quadling <rquadling@gmail.com> wrote:
> On 16 July 2010 02:42, Joel Perras <joel.perras@gmail.com> 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)
>> {
>> zval *values, *keys;
>> HashPosition pos_values, pos_keys;
>> zval **entry_keys, **entry_values;
>> int num_keys, num_values;
>>
>> if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "aa", &keys,
>> &values) == FAILURE) {
>> return;
>> }
>>
>> num_keys = zend_hash_num_elements(Z_ARRVAL_P(keys));
>> num_values = zend_hash_num_elements(Z_ARRVAL_P(values));
>>
>> if (num_keys != num_values) {
>> php_error_docref(NULL TSRMLS_CC, E_WARNING, "Both parameters
>> should
>> have an equal number of elements");
>> RETURN_FALSE;
>> }
>>
>> if (!num_keys) {
>> php_error_docref(NULL TSRMLS_CC, E_WARNING, "Both parameters
>> should
>> have at least 1 element");
>> RETURN_FALSE;
>> }
>>
>> // [snip]
>>
>>
>> There is currently one open ticket regarding this behaviour
>> (http://bugs.php.net/bug.php?id=34857) 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=29972). 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ël.
>>
>> 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 andrey
>
> "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() === array_combine(array(), array())
>
> Regards,
>
> Richard.
>
> [1] http://svn.php.net/viewvc?view=revision&revision=111688
>
--
I do know everything, just not all at once. It's a virtual memory problem.
You should follow me on Twitter: http://twitter.com/jperras