Iterator Utility¶
Helpers for iterate and handle throught lists.
-
class
Jar\Utilities\Utilities\
IteratorUtility
¶
-
Jar\Utilities\Utilities\IteratorUtility::
sortArrayByColumn
(&$arr, $col, $dir = SORT_ASC)¶ Sort array by column/key value.
Parameters: - $arr (array) – Reference of array.
- $col (string) – The column/key name.
- $dir (int) – The direction SORT_ASC or SORT_DESC
Example:
$persons = [ [ 'name' => 'peter', 'age' => 34 ], [ 'name' => 'klaus', 'age' => 21 ], [ 'name' => 'michael', 'age' => 17 ], ]; IteratorUtility::sortArrayByColumn($persons, 'age');
returns
[ [ 'name' => 'michael' 'age' => 17 ], [ 'name' => 'klaus' 'age' => 21 ], [ 'name' => 'peter' 'age' => 34 ] ]
-
Jar\Utilities\Utilities\IteratorUtility::
extractValuesViaGetMethod
($listOfObjects, $methodName)¶ Extracts properties from objects via their get method.
Parameters: - $listOfObjects (array) – List of objects.
- $methodName (string) – The name of the method, without the beginning ‘get’.
Returns: Extracted values.
Example:
class Person { private string $name; function __construct($name) { $this->name = $name; } public function getName(): string { return $this->name; } }; /* ... */ $persons = [ new Person('klaus'), new Person('peter')]; $names = IteratorUtility::extractValuesViaGetMethod($persons, 'name'); var_dump($names);
returns
['klaus', 'peter']
-
Jar\Utilities\Utilities\IteratorUtility::
extractValuesViaGetMethodFlattened
($listOfObjects, $methodName, $keepKeys = false)¶ Extracts properties from objects via their get method and flattens the result.
Parameters: - $listOfObjects (array) – List of objects.
- $methodName (string) – The name of the method, without the beginning ‘get’.
- $keepKeys (bool) – Keep keys in result. Will overwrite existing values for this key.
Returns: Extracted flattened values.
Example:
class Element { private array $items; function __construct($items) { $this->items = $items; } public function getItems(): array { return $this->items; } }; /* ... */ $elements = [ new Element( [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 ] ), new Element([ 'e' => 5, 'f' => 6, 'g' => 7, 'a' => 8 ]) ]; // without "keepKeys" $allItems = IteratorUtility::extractValuesViaGetMethodFlattened($elements, 'items'); var_dump($allItems); /* Result: 0 => 1 1 => 2 2 => 3 3 => 4 4 => 5 5 => 6 6 => 7 7 => 8 */ // with "keepKeys" $allItems = IteratorUtility::extractValuesViaGetMethodFlattened($elements, 'items', true); var_dump($allItems); /* Result: 'a' => 8 'b' => 2 'c' => 3 'd' => 4 'e' => 5 'f' => 6 'g' => 7 */
-
Jar\Utilities\Utilities\IteratorUtility::
callMethod
($listOfObjects, $method)¶ Calls a method in each object and returns the results.
Parameters: - $listOfObjects (array) – List of objects.
- $method (string) – name of the method.
Returns: List of method results.
Example:
class Element { private int $number; function __construct($number) { $this->number = $number; } public function add(): int { return $this->number + 1; } }; /* ... */ $numbers = [ new Element(5), new Element(2) ]; $addedNumbers = IteratorUtility::callMethod($numbers, 'add'); var_dump($addedNumbers);
returns
[6, 3]
-
Jar\Utilities\Utilities\IteratorUtility::
compact
($arr)¶ Returns a copy of the list with all falsy values (null, 0, ‘’) removed.
Parameters: - $arr (array) – List with values.
Returns: List without falsy values
Example:
IteratorUtility::compact(['hello', null, '', 0, 'world'])
returns
['hello', 'world']
-
Jar\Utilities\Utilities\IteratorUtility::
flatten
($arr)¶ Flattens a nested array.
Parameters: - $arr (array) – The nested array.
Returns: The flat array.
Example:
IteratorUtility::flatten([ [1, 2, 3], [4, 5], [6] ]);
returns
[1, 2, 3, 4, 5, 6]
-
Jar\Utilities\Utilities\IteratorUtility::
filter
($arr, $func)¶ Filters a list with a closure condition
Parameters: - $arr (array) – The array.
- $func (callable) – The filter closure.
Returns: The filtered result.
Example:
IteratorUtility::filter( [1, 2, 3, 4, 5, 6], function($value) { return (bool) ($value % 2); } );
returns
[1, 3, 5]
-
Jar\Utilities\Utilities\IteratorUtility::
map
($arr, $func)¶ Iterates each item and maps the new value throught a function.
Parameters: - $arr (array) – The array.
- $func The transformation function (receives three parametes (callable) –
- value, 2. key, 3. current transformated list).
Returns: The mapped array.
Example:
IteratorUtility::map( [1, 2, 3, 4, 5, 6], function($value) { return $value * 2; } );
returns
[2, 4, 6, 8, 10, 12]
-
Jar\Utilities\Utilities\IteratorUtility::
first
($arr)¶ Returns the first element from a list.
Parameters: - $arr (array) – The array.
Returns: First element.
Example:
IteratorUtility::first(['hello', 'world'])
returns
'hello'
-
Jar\Utilities\Utilities\IteratorUtility::
pluck
($arr, $key)¶ Extracts a the value of a certain key.
Parameters: - $arr (array) – The array.
- $key (string) – The key.
Returns: The extracted values.
Example:
$persons = [ [ 'name' => 'peter', 'age' => 34 ], [ 'name' => 'klaus', 'age' => 21 ], [ 'name' => 'michael', 'age' => 17 ], ]; IteratorUtility::pluck($persons, 'name');
returns
['peter' , 'klaus' , 'michael']
-
Jar\Utilities\Utilities\IteratorUtility::
contains
($arr, $needle)¶ Checks if a value exist in an array.
Parameters: - $arr (array) – The array.
- $needle (string) – The value to check.
Returns: Check result.
Example:
// true IteratorUtility::contains([1, 2, 3, 4, 5, 6], 2); // false IteratorUtility::contains([1, 2, 3, 4, 5, 6], 7);
-
Jar\Utilities\Utilities\IteratorUtility::
whitelist
($array, $whitelist)¶ Returns only array entries listed in a whitelist.
Parameters: - $array (array) – Original array to operate on.
- $whitelist (array) – Keys you want to keep.
Returns: The whitelisted entries.
Example:
IteratorUtility::whitelist([ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 ], ['a', 'c']);
returns
[ 'a' => 1, 'c' => 3 ]
-
Jar\Utilities\Utilities\IteratorUtility::
whitelistList
($array, $whitelist)¶ Returns only nested array entries listed in a whitelist.
Parameters: - $array (array) – List of nested array items
- $whitelist (array) – Keys you want to keep.
Returns: The whitelisted entries.
Example:
IteratorUtility::whitelistList([ [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 ], [ 'a' => 5, 'b' => 6, 'c' => 7, 'd' => 8 ] ], ['a', 'c'])
returns
[ [ 'a' => 1, 'c' => 3 ], [ 'a' => 5, 'c' => 7 ] ]
-
Jar\Utilities\Utilities\IteratorUtility::
indexBy
($arr, $key)¶ Create a indexed list based on key values.
Parameters: - $array (array) – The array.
- $key (string) – The index key name.
Returns: The indexed List.
Example:
$persons = [ [ 'id' => 5, 'name' => 'peter', ], [ 'id' => 2, 'name' => 'klaus', ], [ 'id' => 3, 'name' => 'michael', ], ]; var_dump($persons); /* Result: [ 0 => [ 'id' => 5 'name' => 'peter' ], 1 => [ 'id' => 2 'name' => 'klaus' ], 2 => [ 'id' => 3 'name' => 'michael' ] ] */ $indexedPersons = IteratorUtility::indexBy($persons, 'id'); var_dump($indexedPersons); /* Result: [ 5 => [ 'id' => 5 'name' => 'peter' ], 2 => [ 'id' => 2 'name' => 'klaus' ], 3 => [ 'id' => 3 'name' => 'michael' ] ] */