A PHP array manipulation library. Compatible with PHP 7+
Arrayy::create(['Array', 'Array'])->unique()->append('y')->implode() // Arrayy
- Instance methods
- "set an array value"
- "set an array value via dot-notation"
- "get an array value"
- "get an array value via dot-notation"
- "get the array"
- "delete an array value"
- "check if an array value is-set"
- "simple loop with an Arrayy-object"
- append
- prepend
- at
- average
- chunk
- clean
- clear
- customSortKeys
- customSortValues
- contains
- containsKey
- diff
- diffReverse
- diffRecursive
- each
- exists
- filter
- filterBy
- find
- first
- firstsMutable
- firstsImmutable
- flip
- getColumn
- getIterator
- implode
- initial
- intersection
- intersects
- isAssoc
- isMultiArray
- keys
- last
- lastsImmutable
- lastsMutable
- length
- max
- matches
- matchesAny
- mergeAppendKeepIndex
- mergePrependKeepIndex
- mergeAppendNewIndex
- mergePrependNewIndex
- min
- randomKey
- randomKeys
- randomValue
- randomValues
- randomImmutable
- randomMutable
- randomWeighted
- reduce
- reindex
- reject
- remove
- removeFirst
- removeLast
- removeValue
- replace
- replaceAllKeys
- replaceAllValues
- replaceKeys
- replaceOneValue
- replaceValues
- rest
- reverse
- searchIndex
- searchValue
- sort
- sorter
- sortKeys
- sortValueNewIndex
- sortValueKeepIndex
- split
- shuffle
- toJson
- unique
- values
- walk
- Tests
- License
composer require voku/arrayy
If you're using Composer to manage dependencies, you can include the following in your composer.json file:
"require": {
"voku/arrayy": "5.*"
}
Then, after running composer update
or php composer.phar update
, you can
load the class using Composer's autoloading:
require 'vendor/autoload.php';
And in either case, I'd suggest using an alias.
use Arrayy\Arrayy as A;
You can access / change the array via Object, Array or with "Arrayy"-syntax.
-- Recommended --
$arrayy = new A(['Lars' => ['lastname' => 'Moelleken']]);
$arrayy->get('Lars'); // ['lastname' => 'Moelleken']
$arrayy->get('Lars.lastname'); // 'Moelleken'
$arrayy = new A(['Lars' => ['lastname' => 'Moelleken']]);
$arrayy['Lars']; // ['lastname' => 'Moelleken']
$arrayy['Lars']['lastname']; // 'Moelleken'
$arrayy = new A(['Lars' => ['lastname' => 'Moelleken']]);
$arrayy->Lars; // Arrayy['lastname' => 'Moelleken']
$arrayy->Lars->lastname; // 'Moelleken'
-- Recommended --
$arrayy = new A(['Lars' => ['lastname' => 'Mueller']]);
$arrayy->set('Lars.lastname', 'Moelleken');
$arrayy->get('Lars.lastname'); // 'Moelleken'
$arrayy = new A(['Lars' => ['lastname' => 'Moelleken']]);
$arrayy['Lars'] = array('lastname' => 'Müller');
$arrayy['Lars']['lastname]; // 'Müller'
$arrayy = new A(['Lars' => ['lastname' => 'Moelleken']]);
$arrayy->Lars = array('lastname' => 'Müller');
$arrayy->Lars->lastname; // 'Müller'
The library offers OO method chaining, as seen below:
echo a(['fòô', 'bàř', 'bàř'])->unique()->reverse()->implode(','); // 'bàř,fòô'
Arrayy\Arrayy
implements the IteratorAggregate
interface, meaning that
foreach
can be used with an instance of the class:
$arrayy = a(['fòôbàř', 'foo']);
foreach ($arrayy as $value) {
echo $value;
}
// 'fòôbàř'
// 'foo'
It implements the Countable
interface, enabling the use of count()
to
retrieve the number of elements in the array:
$arrayy = a(['fòô', 'foo']);
count($arrayy); // 2
As of PHP 5.6, use function
is
available for importing functions. Arrayy exposes a namespaced function,
Arrayy\create
, which emits the same behaviour as Arrayy\Arrayy::create()
.
If running PHP 5.6, or another runtime that supports the use function
syntax,
you can take advantage of an even simpler API as seen below:
use function Arrayy\create as a;
// Instead of: A::create(['fòô', 'bàř'])->reverse()->implode();
a(['fòô', 'bàř'])->reverse()->implode(','); // 'bàř,fòô'
All methods listed under "Instance methods" are available as part of a static wrapper.
use Arrayy\StaticArrayy as A;
// Translates to Arrayy::create(['fòô', 'bàř'])->reverse();
// Returns an Arrayy object with the array
A::reverse(['fòô', 'bàř']);
Creates an Arrayy object.
$arrayy = new Arrayy(array('fòô', 'bàř')); // Arrayy['fòô', 'bàř']
Creates an Arrayy object, via static "create()"-method
$arrayy = A::create(array('fòô', 'bàř')); // Arrayy['fòô', 'bàř']
WARNING: Creates an Arrayy object by reference.
$array = array('fòô', 'bàř');
$arrayy = A::createByReference($array); // Arrayy['fòô', 'bàř']
Create an new Arrayy object via JSON.
$str = '{"firstName":"John", "lastName":"Doe"}';
$arrayy = A::createFromJson($str); // Arrayy['firstName' => 'John', 'lastName' => 'Doe']
Create an new instance filled with values from an object that have implemented ArrayAccess.
$object = A::create(1, 'foo');
$arrayy = A::createFromObject($object); // Arrayy[1, 'foo']
Create an new instance filled with values from an object.
$object = new stdClass();
$object->x = 42;
$arrayy = A::createFromObjectVars($object); // Arrayy['x' => 42]
Create an new instance containing a range of elements.
$arrayy = A::createWithRange(2, 4); // Arrayy[2, 3, 4]
Create an new Arrayy object via string.
$arrayy = A::createFromString(' foo, bar '); // Arrayy['foo', 'bar']
Arrayy: All examples below make use of PHP 5.6 function importing, and PHP 5.4 short array syntax. For further details, see the documentation for the create method above, as well as the notes on PHP 5.6 creation.
$arrayy = a(['fòô' => 'bàř']);
$arrayy['foo'] = 'bar';
var_dump($arrayy); // Arrayy['fòô' => 'bàř', 'foo' => 'bar']
$arrayy = a(['fòô' => 'bàř']);
var_dump($arrayy['fòô']); // 'bàř'
$arrayy = a(['fòô' => 'bàř']);
var_dump($arrayy->getArray()); // ['fòô' => 'bàř']
$arrayy = a(['fòô' => 'bàř', 'lall']);
unset($arrayy['fòô']);
var_dump($arrayy); // Arrayy[0 => 'lall']
$arrayy = a(['fòô' => 'bàř']);
isset($arrayy['fòô']); // true
$arrayy = a(['fòô' => 'bàř']);
foreach ($arrayy) as $key => $value) {
echo $key . ' | ' . $value; // fòô | bàř
}
Append a value to the current array.
alias: "Arrayy->add()"
a(['fòô' => 'bàř'])->append('foo'); // Arrayy['fòô' => 'bàř', 0 => 'foo']
Prepend a value to the current array.
a(['fòô' => 'bàř'])->prepend('foo'); // Arrayy[0 => 'foo', 'fòô' => 'bàř']
Iterate over the current array and execute a callback for each loop.
$result = A::create();
$closure = function ($value, $key) use ($result) {
$result[$key] = ':' . $value . ':';
};
a(['foo', 'bar' => 'bis'])->at($closure); // Arrayy[':foo:', 'bar' => ':bis:']
Returns the average value of the current array.
a([-9, -8, -7, 1.32])->average(2); // -5.67
Create a chunked version of the current array.
a([-9, -8, -7, 1.32])->chunk(2); // Arrayy[[-9, -8], [-7, 1.32]]
Clean all falsy values from the current array.
a([-8 => -9, 1, 2 => false])->clean(); // Arrayy[-8 => -9, 1]
WARNING!!! -> Clear the current array.
a([-8 => -9, 1, 2 => false])->clear(); // Arrayy[]
Custom sort by index via "uksort".
$callable = function ($a, $b) {
if ($a == $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
};
$arrayy = a(['three' => 3, 'one' => 1, 'two' => 2]);
$resultArrayy = $arrayy->customSortKeys($callable); // Arrayy['one' => 1, 'three' => 3, 'two' => 2]
Custom sort by value via "usort".
$callable = function ($a, $b) {
if ($a == $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
};
$arrayy = a(['three' => 3, 'one' => 1, 'two' => 2]);
$resultArrayy = $arrayy->customSortValues($callable); // Arrayy['one' => 1, 'two' => 2, 'three' => 3]
Check if an item is in the current array.
alias: "Arrayy->containsValue()"
a([1, true])->contains(true); // true
Check if all given needles are present in the array.
a([1, true])->containsValues(array(1, true)); // true
Check if the given key/index exists in the array.
a([1 => true])->containsKey(1); // true
Check if all given needles are present in the array as key/index.
a([1 => true])->containsKeys(array(1 => 0)); // true
Check if an (case-insensitive) string is in the current array.
a(['E', 'é'])->containsCaseInsensitive('É'); // true
Return values that are only in the current array.
a([1 => 1, 2 => 2])->diff([1 => 1]); // Arrayy[2 => 2]
Return values that are only in the new $array.
a([1 => 1])->diffReverse([1 => 1, 2 => 2]); // Arrayy[2 => 2]
Return values that are only in the current multi-dimensional array.
a([1 => [1 => 1], 2 => [2 => 2]])->diffRecursive([1 => [1 => 1]]); // Arrayy[2 => [2 => 2]]
Divide an array into two arrays. One with keys and the other with values.
a(['a' => 1, 'b' => ''])->divide(); // Arrayy[Arrayy['a', 'b'], Arrayy[1, '']]
Iterate over the current array and modify the array's value.
$result = A::create();
$closure = function ($value) {
return ':' . $value . ':';
};
a(['foo', 'bar' => 'bis'])->each($closure); // Arrayy[':foo:', 'bar' => ':bis:']
$callable = function ($value, $key) {
return 2 === $key and 'two' === $value;
};
a(['foo', 2 => 'two'])->exists($callable); // true
Fill the array until "$num" with "$default" values.
a(['bar'])->fillWithDefaults(3, 'foo'); // Arrayy['bar', 'foo', 'foo']
Find all items in an array that pass the truth test.
$closure = function ($value) {
return $value % 2 !== 0;
}
a([1, 2, 3, 4])->filter($closure); // Arrayy[0 => 1, 2 => 3]
Filters an array of objects (or a numeric array of associative arrays) based on the value of a particular property within that.
$array = [
0 => ['id' => 123, 'name' => 'foo', 'group' => 'primary', 'value' => 123456, 'when' => '2014-01-01'],
1 => ['id' => 456, 'name' => 'bar', 'group' => 'primary', 'value' => 1468, 'when' => '2014-07-15'],
];
a($array)->filterBy('name', 'foo'); // Arrayy[0 => ['id' => 123, 'name' => 'foo', 'group' => 'primary', 'value' => 123456, 'when' => '2014-01-01']]
Find the first item in an array that passes the truth test, otherwise return false.
$search = 'foo';
$closure = function ($value, $key) use ($search) {
return $value === $search;
};
a(['foo', 'bar', 'lall'])->find($closure); // 'foo'
Get the first value from the current array and return null if there wasn't a element.
a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->first(); // 'foo'
Get the first value(s) from the current array.
a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->firsts(2); // Arrayy[0 => 'foo', 1 => 'bar']
Get the first value(s) from the current array.
a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->firsts(2); // Arrayy[0 => 'foo', 1 => 'bar']
Exchanges all keys with their associated values in an array.
a([0 => 'foo', 1 => 'bar'])->flip(); // Arrayy['foo' => 0, 'bar' => 1]
Get a value from an array (optional using dot-notation).
$arrayy = a(['user' => ['lastname' => 'Moelleken']]);
$arrayy->get('user.lastname'); // 'Moelleken'
// ---
$arrayy = new A();
$arrayy['user'] = ['lastname' => 'Moelleken'];
$arrayy['user.firstname'] = 'Lars';
$arrayy['user']['lastname'] // Moelleken
$arrayy['user.lastname'] // Moelleken
$arrayy['user.firstname'] // Lars
Returns the values from a single column of the input array, identified by the $columnKey, can be used to extract data-columns from multi-arrays.
a([['foo' => 'bar', 'id' => 1], ['foo => 'lall', 'id' => 2]])->getColumn('foo', 'id'); // Arrayy[1 => 'bar', 2 => 'lall']
Returns a new ArrayyIterator, thus implementing the IteratorAggregate interface.
a(['foo', 'bar'])->getIterator(); // ArrayyIterator['foo', 'bar']
Implodes an array.
a([0 => -9, 1, 2])->implode('|'); // '-9|1|2'
Get everything but the last..$to items.
a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->initial(2); // Arrayy[0 => 'foo']
Return an array with all elements found in input array.
a(['foo', 'bar'])->intersection(['bar', 'baz']); // Arrayy['bar']
Return a boolean flag which indicates whether the two input arrays have any common elements.
a(['foo', 'bar'])->intersects(['föö', 'bär']); // false
Check if we have named keys in the current array.
a(['foo' => 'bar', 2, 3])->isAssoc(); // true
Check if we have named keys in the current array.
a(['💩'])->isEqual(['💩']); // true
Check if the current array is a multi-array.
a(['foo' => [1, 2 , 3]])->isMultiArray(); // true
Check if the current array is sequential [0, 1, 2, 3, 4, 5 ...] or not.
a([0 => 'foo', 1 => 'lall', 2 => 'foobar'])->isSequential(); // true
Get all keys from the current array.
alias: "Arrayy->getKeys()"
$arrayy = a([1 => 'foo', 2 => 'foo2', 3 => 'bar']);
$arrayy->keys(); // Arrayy[1, 2, 3]
Get the last value from the current array.
a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->last(); // 'lall'
Get the last value(s) from the current array.
a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->lasts(2); // Arrayy[0 => 'bar', 1 => 'lall']
Get the last value(s) from the current array.
a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->lasts(2); // Arrayy[0 => 'bar', 1 => 'lall']
Count the values from the current array.
alias: "Arrayy->count()" || "Arrayy->size()"
a([-9, -8, -7, 1.32])->length(); // 4
Get the max value from an array.
a([-9, -8, -7, 1.32])->max(); // 1.32
Check if all items in an array match a truth test.
$closure = function ($value, $key) {
return ($value % 2 === 0);
};
a([2, 4, 8])->matches($closure); // true
Check if any item in an array matches a truth test.
$closure = function ($value, $key) {
return ($value % 2 === 0);
};
a([1, 4, 7])->matches($closure); // true
Merge the new $array into the current array.
- keep key,value from the current array, also if the index is in the new $array
$array1 = [1 => 'one', 'foo' => 'bar1'];
$array2 = ['foo' => 'bar2', 3 => 'three'];
a($array1)->mergeAppendKeepIndex($array2); // Arrayy[1 => 'one', 'foo' => 'bar2', 3 => 'three']
Merge the the current array into the $array.
- use key,value from the new $array, also if the index is in the current array
$array1 = [1 => 'one', 'foo' => 'bar1'];
$array2 = ['foo' => 'bar2', 3 => 'three'];
a($array1)->mergePrependKeepIndex($array2); // Arrayy['foo' => 'bar1', 3 => 'three', 1 => 'one']
Merge the new $array into the current array.
- replace duplicate assoc-keys from the current array with the key,values from the new $array
- create new indexes
$array1 = [1 => 'one', 'foo' => 'bar1'];
$array2 = ['foo' => 'bar2', 3 => 'three'];
a($array1)->mergeAppendNewIndex($array2); // Arrayy[0 => 'one', 'foo' => 'bar2', 1 => three']
Merge the current array into the new $array.
- replace duplicate assoc-keys from new $array with the key,values from the current array
- create new indexes
$array1 = [1 => 'one', 'foo' => 'bar1'];
$array2 = ['foo' => 'bar2', 3 => 'three'];
a($array1)->mergePrependNewIndex($array2); // Arrayy['foo' => 'bar1', 0 => 'three', 1 => 'one']
Get the min value from an array.
a([-9, -8, -7, 1.32])->min(); // -9
Move an array element to a new index.
$arr2 = new A(['A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd', 'E' => 'e']);
$newArr2 = $arr2->moveElement('D', 1); // Arrayy['A' => 'a', 'D' => 'd', 'B' => 'b', 'C' => 'c', 'E' => 'e']
Pick a random key/index from the keys of this array.
alias: "Arrayy->getRandomKey()"
$arrayy = A::create([1 => 'one', 2 => 'two']);
$arrayy->randomKey(); // e.g. 2
Pick a given number of random keys/indexes out of this array.
alias: "Arrayy->getRandomKeys()"
$arrayy = A::create([1 => 'one', 2 => 'two']);
$arrayy->randomKeys(); // e.g. Arrayy[1, 2]
Pick a random value from the values of this array.
alias: "Arrayy->getRandomValue()"
$arrayy = A::create([1 => 'one', 2 => 'two']);
$arrayy->randomValue(); // e.g. 'one'
Pick a given number of random values out of this array.
alias: "Arrayy->getRandomValues()"
$arrayy = A::create([1 => 'one', 2 => 'two']);
$arrayy->randomValues(); // e.g. Arrayy['one', 'two']
Get a random string from an array.
alias: "Arrayy->getRandom()"
a([1, 2, 3, 4])->randomImmutable(2); // e.g.: Arrayy[1, 4]
Get a random string from an array.
a([1, 2, 3, 4])->randomMutable(2); // e.g.: Arrayy[1, 4]
Get a random value from an array, with the ability to skew the results.
a([0 => 3, 1 => 4])->randomWeighted([1 => 4]); // e.g.: Arrayy[4] (has a 66% chance of returning 4)
Reduce the current array via callable e.g. anonymous-function.
function myReducer($resultArray, $value) {
if ($value == 'foo') {
$resultArray[] = $value;
}
return $resultArray;
};
a(['foo', 'bar'])->reduce('myReducer'); // Arrayy['foo']
Create a numerically re-indexed Arrayy object.
a([2 => 1, 3 => 2])->reindex(); // Arrayy[0 => 1, 1 => 2]
Return all items that fail the truth test.
$closure = function ($value) {
return $value % 2 !== 0;
}
a([1, 2, 3, 4])->reject($closure); // Arrayy[1 => 2, 3 => 4]
Remove a value from the current array (optional using dot-notation).
a([1 => 'bar', 'foo' => 'foo'])->remove(1); // Arrayy['foo' => 'foo']
Remove the first value from the current array.
a([1 => 'bar', 'foo' => 'foo'])->removeFirst(); // Arrayy['foo' => 'foo']
Remove the last value from the current array.
a([1 => 'bar', 'foo' => 'foo'])->removeLast(); // Arrayy[1 => 'bar']
Removes a particular value from an array (numeric or associative).
a([1 => 'bar', 'foo' => 'foo'])->removeValue('foo'); // Arrayy[1 => 'bar']
Replace a key with a new key/value pair.
$arrayy = a([1 => 'foo', 2 => 'foo2', 3 => 'bar']);
$arrayy->replace(2, 'notfoo', 'notbar'); // Arrayy[1 => 'foo', 'notfoo' => 'notbar', 3 => 'bar']
Create an array using the current array as values and the other array as keys.
$firstArray = array(
1 => 'one',
2 => 'two',
3 => 'three',
);
$secondArray = array(
'one' => 1,
1 => 'one',
2 => 2,
);
$arrayy = a($firstArray);
$arrayy->replaceAllKeys($secondArray); // Arrayy[1 => "one", 'one' => "two", 2 => "three"]
Create an array using the current array as keys and the other array as values.
$firstArray = array(
1 => 'one',
2 => 'two',
3 => 'three',
);
$secondArray = array(
'one' => 1,
1 => 'one',
2 => 2,
);
$arrayy = a($firstArray);
$arrayy->replaceAllValues($secondArray); // Arrayy['one' => 1, 'two' => 'one', 'three' => 2]
Replace the keys in an array with another set.
WARNING: An array of keys must matching the array's size and order!
a([1 => 'bar', 'foo' => 'foo'])->replaceKeys([1 => 2, 'foo' => 'replaced']); // Arrayy[2 => 'bar', 'replaced' => 'foo']
Replace the first matched value in an array.
$testArray = ['bar', 'foo' => 'foo', 'foobar' => 'foobar'];
a($testArray)->replaceOneValue('foo', 'replaced'); // Arrayy['bar', 'foo' => 'replaced', 'foobar' => 'foobar']
Replace values in the current array.
$testArray = ['bar', 'foo' => 'foo', 'foobar' => 'foobar'];
a($testArray)->replaceValues('foo', 'replaced'); // Arrayy['bar', 'foo' => 'replaced', 'foobar' => 'replacedbar']
Get the last elements from index $from until the end of this array.
a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->rest(2); // Arrayy[0 => 'lall']
Return the array in the reverse order.
a([1, 2, 3])->reverse(); // self[3, 2, 1]
Search for the first index of the current array via $value.
a(['fòô' => 'bàř', 'lall' => 'bàř'])->searchIndex('bàř'); // Arrayy[0 => 'fòô']
Search for the value of the current array via $index.
a(['fòô' => 'bàř'])->searchValue('fòô'); // Arrayy[0 => 'bàř']
Set a value for the current array (optional using dot-notation).
$arrayy = a(['Lars' => ['lastname' => 'Moelleken']]);
$arrayy->set('Lars.lastname', 'Müller'); // Arrayy['Lars', ['lastname' => 'Müller']]]
Get a value from a array and set it if it was not.
WARNING: this method only set the value, if the $key is not already set
$arrayy = a([1 => 1, 2 => 2, 3 => 3]);
$arrayy->setAndGet(1, 4); // 1
$arrayy->setAndGet(0, 4); // 4
Serialize the current arrayy-object.
a([1, 4, 7])->serialize();
Unserialize an string and return this object.
$serialized = a([1, 4, 7])->serialize();
a()->unserialize($serialized);
sort(string|int(SORT_ASC) $direction, int(SORT_REGULAR) $strategy, bool(false) $keepKeys) : Arrayy (Mutable)
Sort the current array and optional you can keep the keys.
- $direction e.g.: [SORT_ASC, SORT_DESC, 'ASC', 'asc', 'DESC', 'desc']
- $strategy e.g.: [SORT_REGULAR, SORT_NATURAL, ...]
a(3 => 'd', 2 => 'f', 0 => 'a')->sort(SORT_ASC, SORT_NATURAL, false); // Arrayy[0 => 'a', 1 => 'd', 2 => 'f']
sorter(null|callable $sorter, string|int(SORT_ASC) $direction, int(SORT_REGULAR) $strategy) : Arrayy (Immutable)
Sort a array by value, by a closure or by a property.
- If the sorter is null (default), the array is sorted naturally.
- Associative (string) keys will be maintained, but numeric keys will be re-indexed.
- $direction e.g.: [SORT_ASC, SORT_DESC, 'ASC', 'asc', 'DESC', 'desc']
- $strategy e.g.: [SORT_REGULAR, SORT_NATURAL, ...]
$testArray = range(1, 5);
$under = a($testArray)->sorter(
function ($value) {
return $value % 2 === 0;
}
);
var_dump($under); // Arrayy[1, 3, 5, 2, 4]
Sort the current array by key by $direction = 'asc' or $direction = 'desc'.
- $direction e.g.: [SORT_ASC, SORT_DESC, 'ASC', 'asc', 'DESC', 'desc']
a([1 => 2, 0 => 1])->sortKeys('asc'); // Arrayy[0 => 1, 1 => 2]
sortValueNewIndex(string|int(SORT_ASC) $direction, int(SORT_REGULAR) $strategy) : Arrayy (Immutable)
Sort the current array by value.
- $direction e.g.: [SORT_ASC, SORT_DESC, 'ASC', 'asc', 'DESC', 'desc']
- $strategy e.g.: [SORT_REGULAR, SORT_NATURAL, ...]
a(3 => 'd', 2 => 'f', 0 => 'a')->sortValueNewIndex(SORT_ASC, SORT_NATURAL); // Arrayy[0 => 'a', 1 => 'd', 2 => 'f']
sortValueKeepIndex(string|int(SORT_ASC) $direction, int(SORT_REGULAR) $strategy) : Arrayy (Immutable)
Sort the current array by value.
- $direction e.g.: [SORT_ASC, SORT_DESC, 'ASC', 'asc', 'DESC', 'desc']
- $strategy e.g.: [SORT_REGULAR, SORT_NATURAL, ...]
a(3 => 'd', 2 => 'f', 0 => 'a')->sortValueNewIndex(SORT_ASC, SORT_REGULAR); // Arrayy[0 => 'a', 3 => 'd', 2 => 'f']
Split an array in the given amount of pieces.
a(['a' => 1, 'b' => 2])->split(2, true); // Arrayy[['a' => 1], ['b' => 2]]
Stripe all empty items.
a(['a' => 1, 'b' => ''])->stripEmpty(); // Arrayy[['a' => 1]]
Swap two values between positions by key.
a(['a' => 1, 'b' => ''])->swap('a', 'b'); // Arrayy[['a' => '', 'b' => 1]]
Shuffle the current array.
a([1 => 'bar', 'foo' => 'foo'])->shuffle(); // e.g.: Arrayy[['foo' => 'foo', 1 => 'bar']]
Convert the current array to JSON.
a(['bar', array('foo')])->toJson(); // '["bar",{"1":"foo"}]'
Return a duplicate free copy of the current array.
a([2 => 1, 3 => 2, 4 => 2])->uniqueNewIndex(); // Arrayy[1, 2]
Return a duplicate free copy of the current array.
a([2 => 1, 3 => 2, 4 => 2])->uniqueNewIndex(); // Arrayy[2 => 1, 3 => 2]
Get all values from a array.
$arrayy = a([1 => 'foo', 2 => 'foo2', 3 => 'bar']);
$arrayyTmp->values(); // Arrayy[0 => 'foo', 1 => 'foo2', 2 => 'bar']
Apply the given function to every element in the array, discarding the results.
$callable = function (&$value, $key) {
$value = $key;
};
$arrayy = a([1, 2, 3]);
$arrayy->walk($callable); // Arrayy[0, 1, 2]
From the project directory, tests can be ran using phpunit
Released under the MIT License - see LICENSE.txt
for details.