ArrayHelper

ArrayHelper is an array utility class for doing all sorts of odds and ends with arrays.

final
since

1.0

Methods

__construct

Private constructor to prevent instantiation of this class

__construct() 
since

1.0

addColumn

Adds a column to an array of arrays or objects

addColumn(array $array, array $column, string $colName, string $keyCol = null) : array
static
since

1.5.0

see

https://secure.php.net/manual/en/language.types.array.php

Arguments

$array

arrayThe source array

$column

arrayThe array to be used as new column

$colName

stringThe index of the new column or name of the new object property

$keyCol

stringThe index of the column or name of object property to be used for mapping with the new column

Response

arrayAn array with the new column added to the source array

arrayFromObject

Utility function to map an object or array to an array

arrayFromObject(mixed $item, boolean $recurse, string $regex) : array
static
since

1.0

Arguments

$item

mixedThe source object or array

$recurse

booleanTrue to recurse through multi-level objects

$regex

stringAn optional regular expression to match on field names

Response

array

arraySearch

An improved array_search that allows for partial matching of strings values in associative arrays.

arraySearch(string $needle, array $haystack, boolean $caseSensitive = true) : mixed
static
since

1.0

Arguments

$needle

stringThe text to search for within the array.

$haystack

arrayAssociative array to search in to find $needle.

$caseSensitive

booleanTrue to search case sensitive, false otherwise.

Response

mixedReturns the matching array $key if found, otherwise false.

arrayUnique

Multidimensional array safe unique test

arrayUnique(array $array) : array
static
see

https://secure.php.net/manual/en/function.array-unique.php

since

1.0

Arguments

$array

arrayThe array to make unique.

Response

array

dropColumn

Remove a column from an array of arrays or objects

dropColumn(array $array, string $colName) : array
static
since

1.5.0

see

https://secure.php.net/manual/en/language.types.array.php

Arguments

$array

arrayThe source array

$colName

stringThe index of the column or name of object property to be removed

Response

arrayColumn of values from the source array

flatten

Method to recursively convert data to a one dimension array.

flatten(array|object $array, string $separator = '.', string $prefix = '') : array
static
since

1.3.0

Arguments

$array

array|objectThe array or object to convert.

$separator

stringThe key separator.

$prefix

stringLast level key prefix.

Response

array

fromObject

Utility function to map an object to an array

fromObject(object $source, boolean $recurse = true, string $regex = null) : array
static
since

1.0

Arguments

$source

objectThe source object

$recurse

booleanTrue to recurse through multi-level objects

$regex

stringAn optional regular expression to match on field names

Response

array

getColumn

Extracts a column from an array of arrays or objects

getColumn(array $array, string $valueCol, string $keyCol = null) : array
static
since

1.0

see

https://secure.php.net/manual/en/language.types.array.php

https://secure.php.net/manual/en/function.array-column.php

Arguments

$array

arrayThe source array

$valueCol

stringThe index of the column or name of object property to be used as value It may also be NULL to return complete arrays or objects (this is useful together with $keyCol to reindex the array).

$keyCol

stringThe index of the column or name of object property to be used as key

Response

arrayColumn of values from the source array

getValue

Utility function to return a value from a named array or a specified default

getValue(array|\ArrayAccess $array, string $name, mixed $default = null, string $type = '') : mixed
static
since

1.0

throws

\InvalidArgumentException

Arguments

$array

array|\ArrayAccessA named array or object that implements ArrayAccess

$name

stringThe key to search for (this can be an array index or a dot separated key sequence as in Registry)

$default

mixedThe default value to give if no key found

$type

stringReturn type for the variable (INT, FLOAT, STRING, WORD, BOOLEAN, ARRAY)

Response

mixed

invert

Takes an associative array of arrays and inverts the array keys to values using the array values as keys.

invert(array $array) : array
static

Example: $input = array( 'New' => array('1000', '1500', '1750'), 'Used' => array('3000', '4000', '5000', '6000') ); $output = ArrayHelper::invert($input);

Output would be equal to: $output = array( '1000' => 'New', '1500' => 'New', '1750' => 'New', '3000' => 'Used', '4000' => 'Used', '5000' => 'Used', '6000' => 'Used' );

since

1.0

Arguments

$array

arrayThe source array.

Response

array

isAssociative

Method to determine if an array is an associative array.

isAssociative(array $array) : boolean
static
since

1.0

Arguments

$array

arrayAn array to test.

Response

boolean

pivot

Pivots an array to create a reverse lookup of an array of scalars, arrays or objects.

pivot(array $source, string $key = null) : array
static
since

1.0

Arguments

$source

arrayThe source array.

$key

stringWhere the elements of the source array are objects or arrays, the key to pivot on.

Response

arrayAn array of arrays pivoted either on the value of the keys, or an individual key of an object or array.

sortObjects

Utility function to sort an array of objects on a given field

sortObjects(array $a, mixed $k, mixed $direction = 1, mixed $caseSensitive = true, mixed $locale = false) : array
static
since

1.0

Arguments

$a

arrayAn array of objects

$k

mixedThe key (string) or an array of keys to sort on

$direction

mixedDirection (integer) or an array of direction to sort in [1 = Ascending] [-1 = Descending]

$caseSensitive

mixedBoolean or array of booleans to let sort occur case sensitive or insensitive

$locale

mixedBoolean or array of booleans to let sort occur using the locale language or not

Response

array

toInteger

Function to convert array to integer values

toInteger(array $array, integer|array $default = null) : array
static
since

1.0

Arguments

$array

arrayThe source array to convert

$default

integer|arrayA default value to assign if $array is not an array

Response

array

toObject

Utility function to map an array to a stdClass object.

toObject(array $array, string $class = 'stdClass', boolean $recursive = true) : object
static
since

1.0

Arguments

$array

arrayThe array to map.

$class

stringName of the class to create

$recursive

booleanConvert also any array inside the main array

Response

object

toString

Utility function to map an array to a string.

toString(array $array, string $innerGlue = '=', string $outerGlue = ' ', boolean $keepOuterKey = false) : string
static
since

1.0

Arguments

$array

arrayThe array to map.

$innerGlue

stringThe glue (optional, defaults to '=') between the key and the value.

$outerGlue

stringThe glue (optional, defaults to ' ') between array elements.

$keepOuterKey

booleanTrue if final key should be kept.

Response

string