# Unintuitive array_combine() behaviour

- Thread: https://externals.io/message/49083
- Original archive: https://news-web.php.net/php.internals/49083
- Started by Joel Perras on 2010-07-16T01:42:46+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #49083 — Joel Perras — 2010-07-16T01:42:46+00:00

URL: https://externals.io/message/49083#49083

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.

---

## #49090 — Richard Quadling — 2010-07-16T09:52:28+00:00

URL: https://externals.io/message/49083#49090 · In reply to: #49083

> 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.
>
> --
> 
> 
>
>

[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

---

## #49545 — Joel Perras — 2010-08-26T17:07:12+00:00

URL: https://externals.io/message/49083#49545 · In reply to: #49090

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.


> 
> > […]
>
> [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

---

## #49547 — Adam Harvey — 2010-08-27T03:55:31+00:00

URL: https://externals.io/message/49083#49547 · In reply to: #49545

> If someone with the appropriate karma could review this and commit it
> to trunk, that would be grand.

Thanks heaps for the patch, Joel. Looked good to me, and IMO the
changed behaviour does make more sense, so I've committed it to trunk.

Thanks again,

Adam
