greeny / array-class
An objective approach to arrays
Installs: 14
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/greeny/array-class
This package is auto-updated.
Last update: 2025-10-19 08:17:18 UTC
README
An objective approach to arrays
The goal
Have you ever used some of PHP's native array_* functions? Then you know that there is some inconsistency,
some of them just return new array, some modify the original one. And not even talking about parameter order
and default values.
Lets take this code:
$array = []; array_push($array, 0, 1, 2, 3); shuffle($array); $array = array_map(function ($item) { return $item * 2; }, $array); $array = array_filter($array); print_r($array);
Can you tell from first look, what the code does? Well you probably can, but the code is messy.
However what if we rewrite the code like this:
$array = ArrayClass::from([]) ->push(0, 1, 2, 3) ->shuffle() ->map(function ($item) { return $item * 2; }) ->filter() ->toArray(); print_r($array);
Looks way more better, right? And quess what, it does the same thing as example above, using same functions, but taking advantages from PHP 5's OOP; PHP 5.6's variadic arguments and other things.
One may argue that working with this class would not be the same as working with arrays, but I have solution for this too!
$array = ArrayClass::from([]); // you can count elements natively count($array); // iterate over elements foreach ($array as $key => $value) { // do stuff } // and even modify some of them! $array[1] = 4; $array[2] = $array[3] * isset($array[4]) ? $array[4] : 4; unset($array[5]);
So you do think it's everything I have for you? Wrong! Look at all these cool features built-in:
- stricter code (every comparsion is done by
===instead of==, that counts also forin_array,searchandkeys) - bigger array-like support: every method that works with plain arrays can also accept
ArrayClassor object implementingTraversable - cloning using
$arrayClass->cloneArray()to return fresh clone or$arrayClass->cloneRef($original)to save original one and return clone - automatic callback checking, throws exception when passed callback is not callable
- nice and easy readable API with fluent interface
- conversion to plain array with
toArray()method - object access (
$arrayClass->foois same as$arrayClass['foo']) - and much more!
So what are you waiting for?
Ah, you maybe do not know, how to initialize that thing?
Well, simply copy the ArrayClass.php file to your working directory and require it:
require_once __DIR__ . '/ArrayClass.php'; // path to the most awesome library ever
Or install it with composer (composer require greeny/array-class) and use composer autoloader:
require_once __DIR__ . '/vendor/autoload.php';
Then you can start enjoying it!
// import class use greeny\ArrayClass\ArrayClass; // yes, I know, weird namespace $array = new ArrayClass; // initializes from empty array $array = new ArrayClass($original); // initializes from original array $array = ArrayClass::from($original); // same as before, good for chaining methods immidiatelly, like below: $array = ArrayClass::from($original)->filter()->shuffle(); // etc etc
Comming soon (maybe)
- unit tests (there may (but should not) be some not yet discovered bugs)
- any ideas? Let me know by opening an issue!
Method index
Almost all of methods provided by ArrayClass just wrap some native PHP function for working with arrays.
If yes, only the name of wrapped PHP function is included, parameters are the same as for original function
(except first array parameter, which is the array you are currently working with, represented by ArrayClass).
changeKeyCase($case) =array_change_key_casechunk($size, $preserveKeys = FALSE) =array_chunkcolumn($key, $indexKey = NULL) =array_columncountValues=array_count_valuesdiffAssoc(...$array) =array_diff_assocdiffKey(...$array) =array_diff_keydiffUassoc($callback, ...$array) =array_diff_uassocdiff(...$array) =array_difffilter($callback = NULL, $flags = 0) =array_filterflip=array_flipintersectAssoc(...$array) =array_intersect_associntersectKey(...$array) =array_intersect_keyintersectUassoc($callback, ...$array) =array_intersect_uassocintersectUkey($callback, ...$array) =array_intersect_ukeyintersect(...$array) =array_intersectkeyExists($key) =array_key_existskeys($search = NULL, $strict = TRUE) =array_keysmap($callback, ...$array) =array_mapmergeRecursive(...$array) =array_merge_recursivemerge(...$array) =array_mergemultisort($order = SORT_ASC, $flags = SORT_REGULAR, ...$args) =array_multisortpad($size, $value) =array_padpop=array_popproduct=array_productpush(...$values) =array_pushrand($num = 1) =array_randreduce($callback, $initial = NULL) =array_reducereplaceRecursive(...$array) =array_replace_recursivereplace(...$array) =array_replacereverse($preserveKeys = FALSE) =array_reversesearch($needle, $strict = TRUE) =array_searchshift=array_shiftslice($offset, $length = NULL, $preserveKeys = FALSE) =array_slicesplice($offset, $length = 0, $replacement = []) =array_splicesum=array_sumudiffAssoc($callable, ...$array) =array_udiff_assocudiffUassoc($valueCallable, $keyCallable, ...$array) =array_udiff_uassocudiff($callable, ...$array) =array_udiffuintersectAssoc($callable, ...$array) =array_uintersect_assocuintersectUassoc($valueCallable, $keyCallable, ...$array) =array_uintersect_uassocuintersect($callable, ...$array) =array_uintersectunique($sort = SORT_STRING) =array_uniqueunshift(...$value) =array_unshiftvalues=array_valueswalkRecursive($callback, $userData = NULL) =array_walk_recursivewalk($callback, $userData = NULL) =array_walkarsort($sort = SORT_REGULAR) =arsortasort($sort = SORT_REGULAR) =asortcurrent=currenteach=eachend=endhas($needle, $strict = TRUE) =in_arraykey=keykrsort($sort = SORT_REGULAR) =krsortksort($sort = SORT_REGULAR) =ksortnatCaseSort=natcasesortnatSort=natsortnext=nextprev=prevreset=resetrsort($sort = SORT_REGULAR) =rsortshuffle=shufflesort($sort = SORT_REGULAR) =sortuasort($callable) =uasortuksort($callable) =uksortusort($callable) =usort
Also there are some utility methods:
combineKeysWith($values) - equals to callingarray_combine($array, $values)combineValuesWith($keys) - equals to callingarray_combine($keys, $array)cloneArray- returns clone ofArrayClasscloneRef(&$ref) - returns clone ofArrayClass, also sets originalArrayClassto$reftoArray- convertsArrayClassto native arraygetIndex($index) - returns value on target indexsetIndex($index, $value) - sets value on target indexissetIndex($index) - determines if index is setunsetIndex($index) - unsets index
And some static methods to start with:
ArrayClass::combine($keys, $values) - creates newArrayClassfrom result ofarray_combine($keys, $values)ArrayClass::fillKeys($array, $value) - creates newArrayClassfrom result ofarray_fill_keys($array, $value)ArrayClass::fill($start, $num, $value) - creates newArrayClassfrom result ofarray_fill($start, $num, $value)ArrayClass::range($start, $end, $step = 1) - creates newArrayClassfrom result ofrange($start, $end, $step)