Array

Class for handling arrays

Arguments

$array
Array to set initial data from.

Example

Combine two arrays together

$values = $this->get('array',Array(one,two))
  ->addArray(Array(three,four))
  ->getArray();

Set data

Create Array

$Array = $this->get('array',$array);

Add values from multidimensional array:

$Array->addArray($otherArray);

Set value to certain key:

$Array->setValue('my/multidimensional/key',$keyValue);

Modify data

Remove keys that has no value:

$Array->removeEmptyKeys();

Run str_replace to all values:

$Array->runStringReplace('',$Order->number);

Set all values to lower case:

$Array->runMofidier('strtolower');

Sort and filter data

Remove rows where key 'status' does not have value 'ordered':

$Array->filterByKeyValue('status','ordered');

Sort array by sum value:

$Array->sortByKey('total_sum');

Get data

Get values from eArray object

$array = $Array->getArray();

Get values in comma -delimited list:

$list = $Array->getValueString(',');

Get the row with certain key/value

$customer = $customerArray->getRowByKeyValue('id','1001');

Get the price for 10kg, from pricing table: array(weight=>price)

$Array = $this->get('array',$weightPricingTable);
$price = $Array->getValueWhereKeyBeforeOr(10);

Full method list

Static helper functions

Method Return Description
count() int Count array, is is array.
keyExists('kay/key', $array) bool Check if is array AND key exists.
keyValue('key/key', $array[,$default='']) mixed Return value if key exists AND has value. Or return default.

THESE SHOUDL NOT BE USED!!

Set data

Method Return Description
setArray() this Clear data and set data to array
addArray() this Add array to data
addValuesByKey(array) this Add key=value -values to array. array(total,100) -> array[total] += 100.

Get data

Method Return Description
getArray() this Return the data as array
getValue(name) mixed Return value in certain key. Name may be multidimensional, / delimiter.
setValue(name) this Set value to certain key. Name may be multidimensional, / delimiter.
getValues() this Get all values (and subvalues) in single list.
getValueString(delimiter) this Get all values (and subvalues) in delimited string. Default delimiter: ,
getRowByKeyValue(key,value)  array Get first row that has given value in given key.
getRowsByKeyValue(key,value)  array Get all rows that has given value in given key. 
getValueWhereKeyBeforeOr(value)  mixed Return value that has a key biggest key which is smaller than or equal to given value.
getValueWhereKeyAfter(value)  mixed Return value that has smallest key which is greater than given value.
getValueWhereKeyAfterOr(value)  mixed Return value that has smallest key which is greater than or equel to given value.

Manipulate data

Method Return Description
runStringReplace(from,to) this Run string replace to all values in array.
runMofidier(funcName) this Run a modifier function for all values in array (also for subvalues)

Manipulate keys

Method Return Description
removeEmptyKeys() this Remove all keys that contains no value.
addPrefix(string) this Add a prefix for array $array[] -> $array[prefix]
toSingleDimension([$delimiter='/']) this Truncate multidimensional array to single dimension. Concatenate keys with delimiter: array[my][multi][dimension] --> array['my/multi/dimension']

Filter and sort

Method Return Description
filterByKeyValue(key,value)  this Remove rows that has not given value in given key.
sortByKey(key[,type])  this Sort array by values in given key. Type: number/string.
rsortByKey(key[,type])  this  Reverse sort array by values in given key. Type: number/string. If type not given, sort type is interpret from first value in key.
groupBy(key) array Return rows grouped by given key. Return groups: [value, data[] ]

Get info

Method Return Description
hasValue(key,value) bool Return true if array contains a value with given value.
isStringInValue(str) bool Return true, if str is found in any of the values.
countKeys()  int Count the number of keys.
countValues()  int Count the number of keys that has a value.
getKeyMin(key)  decimal  Return the min value of given key.
getKeyMax(key)  decimal  Return the max value of given key.
getKeySum(key)  decimal  Return the sum of values in given key.
renderTable() string Return HTML table with keys as column.