# RFC: iterable_to_array() and iterable_count()

- Status: Declined
- Authors: Michael Moravec (php.net@majkl578.cz)
- Date: 2018-06-19
- Version: 1.0
- Original: https://wiki.php.net/rfc/iterable_to_array-and-iterable_count
- Page: https://externals.io/rfc/iterable_to_array-and-iterable_count
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Add iterable_to_array()? (closed)

Yes: 8 · No: 24

Voters: ajf (No), ashnazg (No), bwoebi (No), dams (Yes), dmitry (No), galvao (Yes), gasolwu (No), hywan (Yes), irker (No), kalle (No), kelunik (No), krakjoe (No), lcobucci (Yes), lex (No), marcio (No), mcmic (Yes), mrook (No), narf (No), nikic (No), ocramius (Yes), peehaa (No), pmmaga (No), pollita (No), rtheunissen (No), salathe (No), seld (Yes), subjective (No), svpernova09 (No), trowski (No), weierophinney (No), yunosh (Yes), zimt (No)

### Add iterable_count()? (closed)

Yes: 2 · No: 35

Voters: ajf (No), ashnazg (No), bwoebi (No), cmb (No), colinodell (No), dams (Yes), dmitry (No), galvao (Yes), gasolwu (No), gooh (No), hywan (No), irker (No), jhdxr (No), kalle (No), kelunik (No), krakjoe (No), levim (No), lex (No), marcio (No), mcmic (No), mgocobachi (No), mrook (No), narf (No), nikic (No), ocramius (No), peehaa (No), pmmaga (No), pollita (No), rtheunissen (No), salathe (No), seld (No), subjective (No), svpernova09 (No), trowski (No), weierophinney (No), yunosh (No), zimt (No)

## Discussions on the mailing list

- [\[RFC\]\[Discussion\] iterable_to_array() and iterable_count()](https://externals.io/message/102313.md) — started on 2018-06-20, 3 messages
- [\[RFC\]\[Vote\] iterable_to_array() and iterable_count()](https://externals.io/message/102562.md) — started on 2018-07-03, 11 messages
- [Make iterator_to_array() accept all iterables](https://externals.io/message/117979.md) — started on 2022-06-17, 7 messages

---

## 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 *iterable*s much harder to work with.

In order to improve user experience and ease adoption of *iterable*s, 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>
