Download the PHP package jopic/php-streams without Composer
On this page you can find all versions of the php package jopic/php-streams. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jopic/php-streams
More information about jopic/php-streams
Files in jopic/php-streams
Package php-streams
Short Description Library for Streaming contents of arrays/lists
License Apache-2.0
Informations about the package php-streams
PHP Streams Library
Library for streaming contents of PHP arrays.
Requirements
- PHP version >= 5.3
Installation
Add the following composer dependency: "jopic/php-streams": "1.0.3"
Usage
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
the method allows you to define the maximum number of records used in the result functions (each
, toArray
, collect
)
skip
the method allows you to define the number of matching elements to skip
step (ListStream only)
the method allows you to define the step width function for the for loop
filter
the method allows you to apply a filter method on the stream elements
map
the method allows you to apply a map function on the stream elements
reset
the method resets all actions done with limit
, skip
, step
and filter
each
the method iterates over all matching elements and executes the given function on each of them
Important: The ArrayStream implementation of each
is called with two parameters key
and value
toArray
the method collects all matching elements into a php array
collect (ListStream only)
the method collects all matching elements into a string seperated by the give seperator
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.
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.
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.
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.
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
Example for associative array (filters for key is numeric and returns the matching elements as array
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 & 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
http://www.apache.org/licenses/LICENSE-2.0
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.