Documentation
A (Array)
The a class is a collection of helpers to make things easier with arrays.
Function: a::get
get any value from the array by key
Syntax
a::get('array', 'key'[, 'default']);
Arguments
- array - (array) the array to search in
- key - (string) the key
- default - (mixed) anything that should be returned when nothing is found in the array
Returns
- (mixed) whatever is stored with this key
Example
$array = array(
'cat' => 'miao',
'dog' => 'wuff',
'bird' => 'tweet'
);
echo a::get($array, 'cat');
// output: 'miao'
echo a::get($array, 'elephant', 'shut up');
// output: 'shut up'
Function: a::remove
remove an item from an array
Syntax
a::remove('array', 'key');
Arguments
- array - (array) the array to remove something from
- key - (string) the key
Returns
- (array) the array
Example
$array = array(
'cat' => 'miao',
'dog' => 'wuff',
'bird' => 'tweet'
);
$array = a::remove($array, 'cat');
// array is now: array(
// 'dog' => 'wuff',
// 'bird' => 'tweet'
// );
Function: a::show
a pretty useful debug function to print the content of arrays
Syntax
a::show('array'[, 'echo']);
Arguments
- array - (array) the array to show
- echo - (boolean) true: echo the output immediately, false: return the output
Returns
- (string) the html with all the array content
Example
$array = array(
'cat' => 'miao',
'dog' => 'wuff',
'bird' => 'tweet'
);
a::show($array);
// output:
// Array
// (
// [cat] => miao
// [dog] => wuff
// [bird] => tweet
// )
Function: a::json
Convert the array to a json string
Syntax
a::json('array');
Arguments
- array - (array) the array to convert
Returns
- (string) the json string
Example
$array = array(
'cat' => 'miao',
'dog' => 'wuff',
'bird' => 'tweet'
);
echo a::json($array);
// output: {"cat":"miao","dog":"wuff","bird":"tweet"}
Function: a::xml
Convert the array to xml
Syntax
a::xml('array'[, 'tag'[, 'head'[, 'charset'[,'tab']]]]);
Arguments
- array - (array) the array to convert
- tag - (string) the root tag (default = root)
- head - (boolean) true: a xml header will be added, false: returns just the raw xml
- charset - (string) default is utf-8
- tab - (string) determins the used tab character(s), default is ' '
Returns
- (string) the xml
Example
$array = array(
'cat' => 'miao',
'dog' => 'wuff',
'bird' => 'tweet'
);
echo a::xml($array, 'animals');
// output:
// <animals>
// <cat>miao</cat>
// <dog>wuff</dog>
// <bird>tweet</bird>
// </animals>
Function: a::extract
Extract a certain field from a multidimensional array
Syntax
a::extract('array', 'key');
Arguments
- array - (array) the array to extract from
- key - (string) the name of the key
Returns
- (array) a new array with the extracted values
Example
$array[0] = array(
'id' => 1,
'username' => 'bastian',
);
$array[1] = array(
'id' => 2,
'username' => 'peter',
);
$array[3] = array(
'id' => 3,
'username' => 'john',
);
$extract = a::extract($array, 'username');
// output: array(
// 'bastian',
// 'peter',
// 'john'
// );
Function: a::shuffle
Shuffle an array and keep the key/value pairs
Syntax
a::shuffle('array');
Arguments
- array - (array) the array to shuffle
Returns
- (array) the shuffled array
Example
$array = array(
'cat' => 'miao',
'dog' => 'wuff',
'bird' => 'tweet'
);
$shuffled = a::shuffle($array);
// output: array(
// 'dog' => 'wuff',
// 'cat' => 'miao',
// 'bird' => 'tweet'
// );
Function: a::first
Get the first element of the array
Syntax
a::first('array');
Arguments
- array - (array) the array
Returns
- (mixed) the first element
Example
$array = array(
'cat',
'dog',
'bird',
);
$first = a::first($array);
// first: 'cat'
Function: a::last
Get the last element of the array
Syntax
a::last('array');
Arguments
- array - (array) the array
Returns
- (mixed) the last element
Example
$array = array(
'cat',
'dog',
'bird',
);
$last = a::last($array);
// first: 'bird'
Function: a::search
Search for the given element inside the array
Syntax
a::search('array', 'searchword');
Arguments
- array - (array) the array
- searchword - (string) the searchword
Returns
- (array) returns an array of searchresults
Example
$array = array(
'cat',
'dog',
'bird',
);
$search = a::search($array, 'cat');
// output: array(
// 'cat'
// )
Function: a::contains
Check if the string is contained in the array
Syntax
a::contains('array', 'searchword');
Arguments
- array - (array) the array
- searchword - (string) the searchword
Returns
- (boolean) true: it's inside the array, false: it's not inside
Example
$array = array(
'cat',
'dog',
'bird',
);
if(a::contains($array, 'cat')) echo 'miao';
Function: a::fill
Fill up an array with whatever you like
Syntax
a::fill('array', 'limit'[, 'fill']);
Arguments
- array - (array) the array
- limit - (int) the numer of elements which should be added to the array
- fill - (mixed) the elements which should be added (default: 'placeholder')
Returns
- (array) returns the new array
Example
$array = array(
'cat',
'dog',
'bird',
);
$result = a::fill($array, 5, 'elephant');
// result: array(
// 'cat',
// 'dog',
// 'bird',
// 'elephant',
// 'elephant',
// );
Function: a::missing
Check for missing elements inside an array
Syntax
a::missing('array', 'required');
Arguments
- array - (array) the array
- required - (array) array of required elements to search for
Returns
- (array) returns either an array of missing elements
Example
$array = array(
'cat' => 'miao',
'dog' => 'wuff',
'bird' => 'tweet'
);
$required = array('cat', 'elephant');
$missng = a::missing($array, $required);
// missing: array(
// 'elephant'
// );
Function: a::sort
Sort an mulitdimensional array like you would sort a database table
Syntax
a::sort('array', 'params');
Arguments
- array - (array) the array
- params - (array) the sort parameters
Params
The sort params are pretty similar to MySQL sort params:
fieldname ASC or fieldname DESC
You can even sort by multiple fields:
fieldname1 ASC, fieldname2 DESC
Returns
- (array) returns the sorted array
Example
$array[0] = array(
'id' => 1,
'username' => 'bastian',
);
$array[1] = array(
'id' => 2,
'username' => 'peter',
);
$array[3] = array(
'id' => 3,
'username' => 'john',
);
$sorted = a::sort($array, 'username ASC');
// Array
// (
// [0] => Array
// (
// [id] => 1
// [username] => bastian
// )
// [1] => Array
// (
// [id] => 3
// [username] => john
// )
// [2] => Array
// (
// [id] => 2
// [username] => peter
// )
// )