iterable_to_array() and iterable_count() View as Markdown

Declined
Authors
Michael Moravec (php.net@majkl578.cz)
Date
2018-06-19
Version
1.0
Source
wiki.php.net/rfc/iterable_to_array-and-iterable_count

Votes

Add iterable_to_array()? closed
Yes
8
No
24
32 voters
ajf No 2018-07-09 19:41
ashnazg No 2018-07-09 16:21
bwoebi No 2018-07-10 12:01
dams Yes 2018-07-04 08:21
dmitry No 2018-07-09 21:48
galvao Yes 2018-07-03 22:57
gasolwu No 2018-07-09 03:04
hywan Yes 2018-07-11 13:11
irker No 2018-07-10 08:31
kalle No 2018-07-05 20:47
kelunik No 2018-07-15 11:38
krakjoe No 2018-07-06 08:53
lcobucci Yes 2018-07-04 06:24
lex No 2018-07-09 14:07
marcio No 2018-07-17 11:33
mcmic Yes 2018-07-09 09:23
mrook No 2018-07-07 18:15
narf No 2018-07-09 11:48
nikic No 2018-07-04 21:52
ocramius Yes 2018-07-10 20:26
peehaa No 2018-07-05 21:12
pmmaga No 2018-07-09 20:05
pollita No 2018-07-13 17:44
rtheunissen No 2018-07-06 22:03
salathe No 2018-07-04 20:04
seld Yes 2018-07-05 14:27
subjective No 2018-07-06 21:42
svpernova09 No 2018-07-13 17:54
trowski No 2018-07-09 22:54
weierophinney No 2018-07-09 16:40
yunosh Yes 2018-07-05 20:30
zimt No 2018-07-09 06:44
Add iterable_count()? closed
Yes
2
No
35
37 voters
ajf No 2018-07-09 19:41
ashnazg No 2018-07-09 16:21
bwoebi No 2018-07-10 12:01
cmb No 2018-07-05 15:31
colinodell No 2018-07-06 20:28
dams Yes 2018-07-04 08:21
dmitry No 2018-07-09 21:48
galvao Yes 2018-07-03 22:57
gasolwu No 2018-07-09 03:04
gooh No 2018-07-10 11:09
hywan No 2018-07-11 13:12
irker No 2018-07-10 08:31
jhdxr No 2018-07-08 09:47
kalle No 2018-07-05 20:47
kelunik No 2018-07-07 15:54
krakjoe No 2018-07-06 08:54
levim No 2018-07-06 21:26
lex No 2018-07-09 14:07
marcio No 2018-07-17 11:33
mcmic No 2018-07-09 09:22
mgocobachi No 2018-07-06 20:34
mrook No 2018-07-07 18:15
narf No 2018-07-09 11:48
nikic No 2018-07-04 21:52
ocramius No 2018-07-10 20:26
peehaa No 2018-07-05 21:12
pmmaga No 2018-07-09 20:05
pollita No 2018-07-10 13:24
rtheunissen No 2018-07-06 22:02
salathe No 2018-07-04 20:04
seld No 2018-07-05 14:26
subjective No 2018-07-06 21:41
svpernova09 No 2018-07-13 17:54
trowski No 2018-07-09 22:54
weierophinney No 2018-07-09 16:39
yunosh No 2018-07-05 20:30
zimt No 2018-07-09 06:44

Discussions on the mailing list

[RFC][Discussion] iterable_to_array() and iterable_count()
by Michael Moravec · 3 messages
[RFC][Vote] iterable_to_array() and iterable_count()
by Michael Moravec · 11 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:52 UTC

Introduction

PHP 7.1 added an iterable pseudo-type which is a union of array and Traversable. Although iterable is very useful, sometimes it's necessary to convert it to an array (i.e. for array_*() functions or for compatibility with older code).

Proposal

PHP currently has iterator_to_array(), iterator_count() and iterator_apply(). Unfortunately these functions are incompatible with iterable as they only work with iterators, not arrays.

In order to transform an iterable to an array, one has to write code as such:

is_array($iterable) ? $iterable : iterator_to_array($iterable)
is_array($iterable) ? count($iterable) : iterator_count($iterable)

Besides being error-prone, this code makes iterables much harder to work with.

In order to improve user experience and ease adoption of iterables, two new functions should help mitigate the code above, basically expanding behavior of iterator_to_array() and iterator_count() to work also with arrays.

iterable_to_array()

The following code:

is_array($iterable) ? $iterable : iterator_to_array($iterable)

Is now replaced by simple:

iterable_to_array($iterable)

This function's behavior is similar to iterator_to_array():

  • when an array is given:
    • when $use_keys = true: array is returned as-is, unchanged
    • when $use_keys = false: array is returned as a list, similar to array_values()
  • when an iterator is given:
    • when $use_keys = true: iterator is converted to an array, preserving the keys (same behavior as iterator_to_array())
    • when $use_keys = false: iterator is converted to an array as a list, ignoring the keys (same behavior as iterator_to_array())

In order to stay consistent with iterator_to_array(), the $use_keys behavior is retained.

iterable_count()

The following code:

is_array($iterable) ? count($iterable) : iterator_count($iterable)

Is now replaced by simple:

iterable_count($iterable)

This function's behavior is similar to iterator_count():

  • when an array is given: returns the number of elements contained in the array (same as count())
  • when an iterator is given: returns the number of elements contained in the iterator (same as iterator_count())

Backward Incompatible Changes

None.

Proposed PHP Version(s)

next 7.x

RFC Impact

Two new SPL functions which could collide with user-land functions. Since iterable type is still quite new, the risk should be relatively low.

To SAPIs

None.

To Existing Extensions

None.

To Opcache

None.

Future Scope

Since these new functions provide a superset of features provided by iterator_to_array() and iterator_count(), these could be deprecated later on.

In case these functions gain a popularity, they could be optimized directly by VM, similar to ZEND_COUNT.

Vote

This is not a language change so simple yes/no vote with 50%+1 majority is required.

Voting starts on 2018-07-03 20:30 UTC and closes on 2018-07-16 23:00 UTC.

Vote: Add iterable_to_array()? — Yes: 8 · No: 24 (closed)

.

Vote: Add iterable_count()? — Yes: 2 · No: 35 (closed)

Patches and Tests

GitHub PR: https://github.com/php/php-src/pull/3293