$key_X
$key_X :
Gets content at key X
Enhanced array.
Add content to a collection is easy, and can be done using different ways…
Just use A::add()
method, to append element to the collection:
$a = new A();
$a->add('foo')->add('bar');
// or
$a->push('foo')->push('bar');
You can test whether one given element is included into the collection or not.
exist(mixed $idx) : boolean
Tests whether given index exists.
This checks if given index exists into the collection.
$a = new A();
$a->add('foo');
$a->add('bar');
$a->add('thing');
$a->exist(6); // false
$a->exist(2); // true
mixed | $idx | Value of index, as integer-like |
If given index is not integer-like value.
has(mixed $thing) : boolean
Checks whether collection has given thing or not.
Checks whether given thing is available into the collection.
Example:
$a = new A();
$a->add('foo')->add('bar')->add('thing');
$a->has('bar'); // true
$a->has('other'); // false
mixed | $thing | The element to find. |
add(mixed $thing) : \Malenki\Bah\A
Adds new element into the current collection.
Adds new element at the end of the collection.
Example:
$a = new A();
$a->add('foo')->add('bar')->add('thing');
var_dump($a->array); // 'foo', 'bar', 'thing'
mixed | $thing | The foo element to add… |
push(mixed $thing) : \Malenki\Bah\A
Adds new element into the current collection (Alias).
mixed | $thing | The foo element to add… |
delete(mixed $idx) : \Malenki\Bah\A
Deletes element at given position.
This delete for current position an item at position pos
. So, returned
object is same as current one.
Example:
$a = new A();
$a->add('one')->add('two')->add('three');
echo $a->delete(1)->join(', '); // 'one, three'
mixed | $idx | An integer-like value for position |
If given position is not an integer-like value.
If given position does not exist.
remove(mixed $idx) : \Malenki\Bah\A
Removes element at given position (Alias).
mixed | $idx | An integer-like value for position |
If given position is not an integer-like value.
If given position does not exist.
rm(mixed $idx) : \Malenki\Bah\A
Deletes element at given position (Alias).
mixed | $idx | An integer-like value for position |
If given position is not an integer-like value.
If given position does not exist.
del(mixed $idx) : \Malenki\Bah\A
Deletes element at given position (Alias).
mixed | $idx | An integer-like value for position |
If given position is not an integer-like value.
If given position does not exist.
replace(mixed $idx, mixed $thing) : \Malenki\Bah\A
Replaces element at given position.
Changes one element of the collection by a new one giving a position.
Example:
$a = new A();
$a->add('one')->add('two')->add('three');
$a->replace(1, 'deux')->join(', '); // 'one, deux, three'
mixed | $idx | An integer-like value for position |
mixed | $thing | The thing that replaces foo… :) |
If index is not an integer-like value
If position does not exist
implode(mixed $sep) : \Malenki\Bah\S
Joins all elements into the collection together as a string object.
All elements are converted to sstring, if possible, and joined side by side with given separator.
If some elements are array or A or H object, then they are convert using same method and same séarators.
Example:
$a = new A();
$a->add('one');
$a->add(array('two', 'three'));
$a->add(new S('four'));
$a->add(new A(array('five', 'six')));
$a->add(new H(array('seven', 'eight')));
$a->add(new C('9'));
$a->add(new N(10));
$a->add(11);
echo $a->implode(', '); // 'one, two, three, four, five, six, seven, eight, 9, 10, 11'
mixed | $sep | String-like value as separator |
If separator is not string-like value
If at least one item contains into collection or sub-collection cannot be converted to a string.
join(mixed $sep) : \Malenki\Bah\S
Joins all elements into the collection together as a string object (Alias) .
mixed | $sep | String-like value as separator |
If separator is not string-like value
If at least one item contains into collection or sub-collection cannot be converted to a string.
pad(mixed $size, mixed $value) : \Malenki\Bah\A
Fill the collection with given element on given size.
If current collection already has values, then they are kept.
Example:
$a = new A(array('foo','bar'));
$a->pad(5); // 'foo', 'bar', null, null, null
$a->pad(5, 'thing'); // 'foo', 'bar', 'thing', 'thing', 'thing'
mixed | $size | An integer-like value for the size |
mixed | $value | A value. If not given, null is used. |
find(mixed $what) : \Malenki\Bah\A
find
mixed | $what | Expression as a string-like value |
If expression is not string-like value.
If test use negative number, they are not negative number as index into collection.
map(callable $func) : \Malenki\Bah\A
Applys given function arg on every item of the collection.
Given argument must be a callable function. This function accept as argument item of collection.
The result is returned into new \Malenki\Bah\A
instance.
Example:
$cube = function ($n) {
return $n * $n * $n;
};
$a = new A(range(1, 5));
$a->map($cube)->array; // array(1, 8, 27, 64, 125)
callable | $func |
If param is not callable
random(integer $n) : \Malenki\Bah\A
Gets random items from the collection.
integer | $n | Number of item to take. |
If number of items is not an integer-like value.
If number of items is less than one.
If number of items to take is greater than total available items.
diff(mixed $arr) : \Malenki\Bah\A
Returns elements not in common into current collection and given one.
This method takes one argument: an array-like collection to compare with current one and find elements not in common into second.
Given argument can be \Malenki\Bah\A
object, \Malenki\Bah\H
object
or simple arry.
Example:
$a = new A();
$a->add('one')->add('two');
$b = new A();
$b->add('three', 'one');
$a->diff($b); // 'two'
mixed | $arr | An array-like collection (array, A or H object) |
If given argument is not an array-like value
inter(mixed $arr) : \Malenki\Bah\A
Gets commons elements into currrent collection and given collection.
Same elements found into current collection and given collection are
returned into new \Malenki\Bah\A
instance.
Given collection can be simple array, \Malenki\Bah\A
object or
\Malenki\Bah\H
object.
$a1 = new A();
$a1->add('blue')->add('white')->add('red');
$a2 = new A();
$a2->add('green')->add('white')->add('red');
var_dump($a1->inter($a2)->array); // array('white', 'red')
var_dump($a2->inter($a1)->array); // array('white', 'red')
mixed | $arr | Collection (array or object) |
If argument is not array-like value
merge() : \Malenki\Bah\A
Merge current collection with other(s) .
You can merge with current collection any number of array-like variables.
Example:
$a = new A();
$a->add('one')->add('two');
$b = new A();
$b->add('three');
$c = new A();
$c->add('four')->add('five');
$a->merge($b, $c); // has 'one', 'two', 'three', 'four', 'five'
If at least one collection has bad type
concat() : \Malenki\Bah\A
Merge current collection with other(s) (Alias).
If at least one collection has bad type
chunk(\Malenki\Bah\N|integer $size) : \Malenki\Bah\A
Divides current collection into subcollection having given size.
Collection is splitted into several parts, each of them has size given in argument, the last can have small size.
Example:
$a = new A(array(1, 2, 3, 4, 5, 6, 7, 8, 9));
$a->chunk(3); // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
$a->chunk(4); // [[1, 2, 3, 4], [5, 6, 7, 8], [9]]
\Malenki\Bah\N|integer | $size | Chunk’s size, as an integer-like value. |
If argument is not integer-like value.
If given size is less than one.
search(mixed $foo) : mixed
Search index of the given element.
This returns the first index found for the given element if there are several identicals.
If no element found, then returns null.
If element is found, then N object is returned.
Example:
$a = new A();
$a->add('zéro')->add('un')->add('deux');
echo $a->search('un'); // '1'
mixed | $foo | The element to find |
hasRange(mixed $arr) : boolean
Checks whether current collection has given range inside.
The given range range must be included into the collection without change, into the same order, without break.
Example:
$a = new A(array('un', 'deux', 'trois', 'quatre'));
$a->hasRange(array('deux', 'trois')); // true
$a->hasRange(array('deux', 'un')); // false
mixed | $arr | Array-like collection |
If given collection is a void array (arrays are allowed, but not void)
If given argument is not an array-like value.
zip() : \Malenki\Bah\A
Combines provided collections with current one.
This works like array_combine()
PHP function, but it can use many
collections or arrays.
If some collection have less contents than others, then into combined
result, void value are replaced by null
.
Examples:
$a = new A(array('un', 'deux', 'trois'));
$b = new A(array('a', 'b', 'c'));
$c = new A(array(1, 2, 3));
$a->zip($b, $c);
// will give this structured A objects:
// Row 1: 'un', 'a', 1
// Row 2: 'deux', 'b', 2
// Row 3: 'trois', 'c', 3
$a = new A(array('un', 'deux', 'trois'));
$b = new A(array('a', 'b'));
$c = new A(array(1, 2, 3));
$a->zip($b, $c);
// will give this structured A objects:
// Row 1: 'un', 'a', 1
// Row 2: 'deux', 'b', 2
// Row 3: 'trois', null, 3
If at least one collection is not
array, or \Malenki\BahA
object or \Malenki\Bah\H
object
mustBeStringOrScalar(mixed $arg, string $arg_name) : void
Checks whether given value is string-like or scalar type.
Value must be of string-like type (string or object having
__toString()
method) or a basic scalar type.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeString(mixed $arg, string $arg_name) : void
Checks whether given value is string-like type.
A string-like value is string or object having __toString()
method.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeInteger(mixed $arg, string $arg_name) : void
Checks whether given value is integer-like type.
An integer-like value is integer or \Malenki\Bah\N
object.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeFloat(mixed $arg, string $arg_name) : void
Checks whether given value is float-like type.
A float-like value is float or \Malenki\Bah\N
object.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeDouble(mixed $arg, string $arg_name) : void
Checks whether given value is double-like type.
A double-like value is string or \Malenki\Bah\N
object.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeNumeric(mixed $arg, string $arg_name) : void
Checks whether given value is numeric-like type.
A numeric-like value is numeric (integer, float or double) or
\Malenki\Bah\N
object.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeArray(mixed $arg, string $arg_name) : void
Checks whether given value is an array-like type.
A array-like value is an array or \Malenki\Bah\A
object.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeHash(mixed $arg, string $arg_name) : void
Checks whether given value is a hash-like type.
A hash-like value is an array having named-index or \Malenki\Bah\H
object.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeArrayOrHash(mixed $arg, string $arg_name) : void
Checks whether given value is an array-like or hash-like type.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeCallable(mixed $arg, mixed $arg_name) : void
Checks whether the given argument is callable.
This is usefull to test is an argument can be call as a function. If
this argument is not callable, then this raises an
\InvalidArgumentException
.
mixed | $arg | The value to test |
mixed | $arg_name | Optional name of the value, used into error message |
If arg is not callable
_unique() : \Malenki\Bah\A
Removes any duplicate entries.
This method removes all duplicate entries, so resulting collection has only unique items.
Example:
$a = new A();
$a->add('foo');
$a->add('bar');
$a->add('foo');
$a->add('thing');
var_dump($a->unique->array); // has: 'foo', 'bar', 'thing'
_uniq() : \Malenki\Bah\A
Removes any duplicate entries (Alias).
_shift() : mixed
Shift.
Removes first element of the current collection and returns it.
This is runtime part of magic getter A::$shift
.
Example:
$a = new A();
$a->add('one')->add('two')->add('three');
echo count($a); // 3
echo $a->shift; // 'one'
echo count($a); // 2
If current collection is void.
_pop() : mixed
Pop.
Removes last element of the current collection and returns it.
This is runtime part of magic getter A::$pop
.
Example:
$a = new A();
$a->add('one')->add('two')->add('three');
echo count($a); // 3
echo $a->pop; // 'three'
echo count($a); // 2
If current collection is void.
_array() : array
Converts current collection as a simple array, recursively.
This method converts all current collection as a simple array primitive type, and all sub-collectionis included into current one are also converted to array: this is recursive.
Example:
$a = new A();
$a->add('foo')->add('bar');
$a->add(new A(array('other', 'thing'))) // works with H too
$a->array; // array('foo', 'bar', array('other', 'thing'))
_shuffle() : \Malenki\Bah\A
Returns a shuffled copy of the current collection.
This runtime part of magic getter A::$shuffle
randomize collection’s
content to a new collection object.
Example:
$a = new A(array('one', 'two', 'three'));
echo $a->shuffle->join(', '); // 'three, one, two' or other order…
_reverse() : \Malenki\Bah\A
Reverses items’ order as new collection.
Returns new collection having current items’ order reversed.
Example:
$a = new A();
$a->add('one')->add('two')->add('three');
var_dump($a->reverse->array); // 'three', 'two', 'one'
This is runtime par of magic getter.
_sort() : \Malenki\Bah\A
Sort elements of the collection.
Sort all elements of the collection by returning new \Malenki\Bah\A
object.
Example:
$a = new A(array('blue', 'white', 'red'));
echo $a->sort->join(', '); 'blue, red, white'
This is runtime part of magic getter.
_flatten() : \Malenki\Bah\A
Flatten the current collection.
If collection contains other arrays or A objects or H objects, then this method will take all this content to create unqiue collection having one dimension.
Example:
$a = new A(array('one', array('two', 'three', new A(array('four', 'five')))));
var_dump($a->flatten->array); // has: 'one', 'two', 'three', 'four', 'five'
_flat() : \Malenki\Bah\A
Flatten the current collection (Alias).
mergeEngine(array $args) : \Malenki\Bah\A
Merge current collection with other(s) .
This is runtime part of A::merge()
and A::concat()
.
array | $args | All collection to merge into array |
If at least one collection has bad type