PHPackages                             jopic/php-streams - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jopic/php-streams

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jopic/php-streams
=================

Library for Streaming contents of arrays/lists

1.0.3(10y ago)621Apache-2.0PHP

Since Dec 3Pushed 10y ago1 watchersCompare

[ Source](https://github.com/fetzi/php-streams)[ Packagist](https://packagist.org/packages/jopic/php-streams)[ RSS](/packages/jopic-php-streams/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (4)Dependencies (2)Versions (5)Used By (0)

PHP Streams Library
===================

[](#php-streams-library)

[![Build Status](https://camo.githubusercontent.com/01ada1d534a5606680735748755e2b9b5777ff1c1fe7954702cdd251b0042ffb/68747470733a2f2f7472617669732d63692e6f72672f6665747a692f7068702d73747265616d732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/fetzi/php-streams)

Library for streaming contents of PHP arrays.

Requirements
------------

[](#requirements)

- PHP version &gt;= 5.3

Installation
------------

[](#installation)

Add the following composer dependency: `"jopic/php-streams": "1.0.3"`

Usage
-----

[](#usage)

### Initialization

[](#initialization)

The library distinguishes between Lists (array with numeric indices) and and other Arrays. Therefore two initializations are possible

- `$stream = Stream::ofList(array(1, 2, 3));` for Lists
- `$arrayStream = Stream::ofArray(array("key1" => "value1""));` for array with different key types

### limit

[](#limit)

the method allows you to define the maximum number of records used in the result functions (`each`, `toArray`, `collect`)

```
Stream::ofList(array(1, 2, 3))
    ->limit(1) // limits the stream to the first element
```

### skip

[](#skip)

the method allows you to define the number of matching elements to skip

```
Stream::ofList(array(1, 2, 3))
    ->skip(1) // the resulting elements are (2, 3)
```

### step (ListStream only)

[](#step-liststream-only)

the method allows you to define the step width function for the for loop

```
Stream::ofList(array(1, 2, 3, 4, 5))
    ->step(function($i) { return $i + 2; }); // will iterate over the following elements (1, 3, 5)
```

### filter

[](#filter)

the method allows you to apply a filter method on the stream elements

```
Stream::ofList(array(1, 2, 3, 4, 5))
    ->filter(function($item) { return $item % 2 == 0; }); // will filter the elements (2, 4)
```

### map

[](#map)

the method allows you to apply a map function on the stream elements

```
Stream::ofList(array(1, 2, 3))
    ->map(function($item) { return $item + 1; })
    ->toArray(); // will return the array(2, 3, 4)
```

### reset

[](#reset)

the method resets all actions done with `limit`, `skip`, `step` and `filter`

```
Stream::ofList(array(1, 2, 3, 4, 5))
    ->filter(function($item) { return $item % 2 == 0; }) // matching elements (2, 4)
    ->reset(); // matching elements (1, 2, 3, 4, 5)
```

### each

[](#each)

the method iterates over all matching elements and executes the given function on each of them

```
Stream::ofList(array(1, 2, 3, 4, 5))
    ->each(function($item) { echo $item; }); // will print all items
```

**Important**: The ArrayStream implementation of `each` is called with two parameters `key` and `value`

### toArray

[](#toarray)

the method collects all matching elements into a php array

```
Stream::ofList(array(1, 2, 3, 4, 5))
    ->skip(2)
    ->limit(2)
    ->toArray(); // will return array(3, 4)
```

### collect (ListStream only)

[](#collect-liststream-only)

the method collects all matching elements into a string seperated by the give seperator

```
Stream::ofList(array(1, 2, 3))
    ->collect(','); // will return "1,2,3"
```

### sum

[](#sum)

the method sums up all matching elements. This functionality is available either when all values within the array are of the same numeric datatype or if a `map` function is defined.

```
Stream::ofList(array(1, 2, 3))
    ->sum(); // will return 6
```

### min

[](#min)

the method returns the minimum value of all matching elements. This functionality is available either when all values within the array are of the same numeric datatype or if a `map` function is defined.

```
Stream::ofList(array(1, 2, 3))
    ->min(); // will return 1
```

### max

[](#max)

the method returns the maximum value of all matching elements. This functionality is available either when all values within the array are of the same numeric datatype or if a `map` function is defined.

```
Stream::ofList(array(1, 2, 3))
    ->max(); // will return 3
```

### avg

[](#avg)

the method returns the average value of all matching elements. This functionality is available either when all values within the array are of the same numeric datatype or if a `map` function is defined.

```
Stream::ofList(array(1, 2, 3, 4))
    ->avg(); // will return 2.5
```

Examples
--------

[](#examples)

Example to skip the first element, filter for odd values, limit the result to 3 and collect the matching elements into a concatinated string

```
Stream::ofList(array(1, 2, 3, 4, 5, 6))
    ->skip(1)
    ->filter(function($item) {
        return $item % 2 == 1;
    })
    ->limit(3)
    ->collect(', ') // will return "3, 5"
```

Example for associative array (filters for key is numeric and returns the matching elements as array

```
Stream::ofArray(array(
        "key1" => "value1",
        "key2" => 2,
        "key3" => 3,
        4 => "value 4"
    ))
    ->filter(function($key, $value) {
        return is_numeric($key);
    })
    ->toArray(); // will return array(4 => "value 4")
```

Issues or Improvements
----------------------

[](#issues-or-improvements)

If you find any issues or have ideas how to improve the library don't hesitate to open an issue on the github project.

Copyright &amp; License
-----------------------

[](#copyright--license)

Copyright 2015 Johannes Pichler

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

4

Last Release

3858d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b155d5296b644a0e937677b500bee7983ce4a91294b616d7a690e57f5b14a3aa?d=identicon)[fetzi](/maintainers/fetzi)

---

Top Contributors

[![fetzi](https://avatars.githubusercontent.com/u/3509426?v=4)](https://github.com/fetzi "fetzi (4 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jopic-php-streams/health.svg)

```
[![Health](https://phpackages.com/badges/jopic-php-streams/health.svg)](https://phpackages.com/packages/jopic-php-streams)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
