\cge_array

Utility methods to deal with arrays.

Summary

Methods
Properties
Constants
insert_unique()
key_exists_substr()
find_key_regexp()
merge_by_keys()
to_hash()
extract_field()
compare_elements_by_sortorder_key()
hashsort()
objsort()
hashrsort()
explode_with_key()
find_index()
implode_with_key()
implode_quoted()
str_putcsv()
to_object()
hash_prepend()
remove_by_value()
is_hash()
explode_to_tree()
smart_explode()
search_recursive()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

insert_unique()

insert_unique(array  $arr, mixed  $val) : array

A functon to test if an array element exists and if it does not, add the value specified.

Parameters

array $arr

The input array

mixed $val

Returns

array

key_exists_substr()

key_exists_substr(array  $arr, string  $expr) : boolean

A method to test if an array element exists by testing a subset of it's key.

Parameters

array $arr

The array to test

string $expr

The substring expression to test for

Returns

boolean

find_key_regexp()

find_key_regexp(array  $arr, string  $expr) : mixed

Test if an array key exists, given a regular expression.

Parameters

array $arr

The array to search

string $expr

The regular expression to use in the search

Returns

mixed —

FALSE or actual key name.

merge_by_keys()

merge_by_keys(array  $arr1, array  $arr2, string  $key1 = 'name', string  $key2 = 'name') 

Merge two arrays of hashes based on certain keys

Parameters

array $arr1

The primary array

array $arr2

The array to be merged

string $key1

The key field in the first array

string $key2

The key field in the second array

to_hash()

to_hash(array  $input, string  $key) : array

re-arrange an array of arrays into a hash of arrays by a specified key.

Parameters

array $input

The input array

string $key

The key to create the output array on

Returns

array

extract_field()

extract_field(array  $input, string  $key) : array

Extract one field from an array of hashes into a flat array

Parameters

array $input

The input associative array

string $key

The key to extract from the array of hashes

Returns

array

compare_elements_by_sortorder_key()

compare_elements_by_sortorder_key(array  $e1, array  $e2, string  $key = 'sort_key') : integer

Compare two hashes by the key 'sort_key' This function is useful for sorting arrays of hashes.

Parameters

array $e1

Associative Array 1

array $e2

Associative Array 2

string $key

The key to sort each element by

Returns

integer —

-1, 0, or -1

hashsort()

hashsort(array  $input, string  $key, boolean  $is_string = false, boolean  $casecompare = false) : boolean

Sort array of hashes by key

Parameters

array $input

An array of hashes. This array of hashes is sorted.

string $key

The hash key to sort by

boolean $is_string

A boolean indicating if the key is a string

boolean $casecompare

If $is_string is true, wether or not to do case sensitive comparisons

Returns

boolean

objsort()

objsort(array  $input, string  $member, boolean  $is_string = false, boolean  $casecompare = false) : boolean

Sort array of objects by member

Parameters

array $input

Arreay of objects to sort

string $member

The member name to sort on

boolean $is_string

Indicates wether the member is a string

boolean $casecompare

Wether the values should be sorted in a case sensitive, or case insensitive manner.

Returns

boolean

hashrsort()

hashrsort(array  $input, string  $key, boolean  $is_string = false, boolean  $casecompare = false) : void

Sort array of hashes by key in reverse order

Parameters

array $input

The input array of hashes. This array is modified on output

string $key

The hash key to sort by

boolean $is_string

Wether the value of the key is a string or not

boolean $casecompare

If the value of the key is a string, wether it should be sorted in a case sensitive manner, or not.

explode_with_key()

explode_with_key(string  $str, string  $inglue = '=', string  $outglue = '&') : array

Explode an array into a hash useful for separating params on a URL into a hash

Parameters

string $str

The input string.

string $inglue

inner glue string (separates name from value)

string $outglue

outer glue string (separates each variable/value combination)

Returns

array

find_index()

find_index(array  $data, mixed  $needle) : \index

Given an array and a value, return the index of that value

Parameters

array $data
mixed $needle

Returns

\index —

or FALSE

implode_with_key()

implode_with_key(\hash  $assoc, string  $inglue = '=', string  $outglue = '&') 

Implode a hash into an array suitable for forming a URL string with multiple key/value pairs

Parameters

\hash $assoc

input hash

string $inglue

inner glue

string $outglue

outer glue

implode_quoted()

implode_quoted(array  $data, string  $glue = ',', string  $quote = '"') : string

Given an array, implode it into a string with non empty values quoted.

Parameters

array $data

The input array

string $glue

How each value should be separated in the input

string $quote

How non empty fields should be deliminated

Returns

string

str_putcsv()

str_putcsv(array  $input, \string  $delimiter = ',', \string  $enclosure = '"') 

Parameters

array $input
\string $delimiter
\string $enclosure

to_object()

to_object(\hash  $array, boolean  $recursive = FALSE) : \stdclass

Convert a hash into a stdclass object

Parameters

\hash $array

input array

boolean $recursive

Wether or not to do recursive conversion

Returns

\stdclass —

object.

hash_prepend()

hash_prepend(\hash  $input, string  $key, mixed  $value) : \hash

Prepend a key/value pair to a hash.

This is useful when prepending an 'any' value to a list retrieved from the database, for use in filtering dialogs etc.

Parameters

\hash $input

input array

string $key

The key to prepend

mixed $value

The value to prepend

Returns

\hash

remove_by_value()

remove_by_value(array  $input, string  $value = '') : array

Remove All elements of the input array that have the specified value

Parameters

array $input

The input hash/array

string $value

The value to remove

Returns

array

is_hash()

is_hash(array  $arr) : boolean

Test if the input array is an associative array (strings or something else for keys, instead of linearly increasing integers)

Parameters

array $arr

The test array

Returns

boolean

explode_to_tree()

explode_to_tree(array  $hash, string  $delimiter = '.', string  $baseval = false) 

Given a hash of records, where the key denotes the path separeted with a delimieter, generate a tree of records.

Parameters

array $hash

The input hash

string $delimiter

The identifier delimiter (used on hash keys)

string $baseval

Internal use only

smart_explode()

smart_explode(string  $str, string  $delim = ',', string  $safe_char = '"') : array

Smartly explode a string into an array given a delimiter, and a safe character.

Any instances of the delimiter inside two 'safe characters' will be included in the value.

This is useful when importing data from CSV.

Parameters

string $str

The input string

string $delim

The field delimeter

string $safe_char

The save character

Returns

array

search_recursive()

search_recursive(array  $input, string  $needle) : mixed

Search a hash (recursively) to find an entry where the key specified is found

Parameters

array $input

A (potentially recursive) hash

string $needle

a member name

Returns

mixed —

The value of the found member