ArrayHelper

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

final
since

1.0

package

Joomla Framework

Methods

__construct

Private constructor to prevent instantiation of this class

__construct() : 
since

1.0

Response

mixed

addColumn

Adds a column to an array of arrays or objects

addColumn(mixed||string|int array, mixed||string|int column,  colName,  keyCol = null) : mixed||string|int
static
since

1.5.0

see

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

Arguments

array

array<string|int, mixed>The source array

column

array<string|int, mixed>The 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

array<string|int, mixed>An array with the new column added to the source array

arrayFromObject

Utility function to map an object or array to an array

arrayFromObject( item,  recurse,  regex) : mixed||string|int
static
since

1.0

Arguments

item

mixedThe source object or array

recurse

boolTrue to recurse through multi-level objects

regex

stringAn optional regular expression to match on field names

Response

array<string|int, mixed>

arraySearch

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

arraySearch( needle, mixed||string|int haystack,  caseSensitive = true) : 
static
since

1.0

Arguments

needle

stringThe text to search for within the array.

haystack

array<string|int, mixed>Associative array to search in to find $needle.

caseSensitive

boolTrue to search case sensitive, false otherwise.

Response

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

arrayUnique

Multidimensional array safe unique test

arrayUnique(mixed||string|int array) : mixed||string|int
static
see

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

since

1.0

Arguments

array

array<string|int, mixed>The array to make unique.

Response

array<string|int, mixed>

dropColumn

Remove a column from an array of arrays or objects

dropColumn(mixed||string|int array,  colName) : mixed||string|int
static
since

1.5.0

see

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

Arguments

array

array<string|int, mixed>The source array

colName

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

Response

array<string|int, mixed>Column of values from the source array

flatten

Method to recursively convert data to a one dimension array.

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

1.3.0

Arguments

array

array<string|int, mixed>|objectThe array or object to convert.

separator

stringThe key separator.

prefix

stringLast level key prefix.

Response

array<string|int, mixed>

fromObject

Utility function to map an object to an array

fromObject( source,  recurse = true,  regex = null) : mixed||string|int
static
since

1.0

Arguments

source

objectThe source object

recurse

boolTrue to recurse through multi-level objects

regex

stringAn optional regular expression to match on field names

Response

array<string|int, mixed>

getColumn

Extracts a column from an array of arrays or objects

getColumn(mixed||string|int array,  valueCol,  keyCol = null) : mixed||string|int
static
since

1.0

see

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

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

Arguments

array

array<string|int, mixed>The 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

array<string|int, mixed>Column 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,  name,  default = null,  type = '') : 
static
since

1.0

throws

InvalidArgumentException

Arguments

array

array<string|int, mixed>|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(mixed||string|int array) : mixed||string|int
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

array<string|int, mixed>The source array.

Response

array<string|int, mixed>

isAssociative

Method to determine if an array is an associative array.

isAssociative(mixed||string|int array) : 
static
since

1.0

Arguments

array

array<string|int, mixed>An array to test.

Response

bool

mergeRecursive

Merge array recursively.

mergeRecursive(mixed||string|int args) : mixed||string|int
static
since

2.0.0

throws

InvalidArgumentException

Arguments

args

array<string|int, mixed>Array list to be merged.

Response

array<string|int, mixed>Merged array.

pivot

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

pivot(mixed||string|int source,  key = null) : mixed||string|int
static
since

1.0

Arguments

source

array<string|int, mixed>The source array.

key

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

Response

array<string|int, mixed>An 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(mixed||string|int a,  k,  direction = 1,  caseSensitive = true,  locale = false) : mixed||string|int
static
since

1.0

Arguments

a

array<string|int, mixed>An 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<string|int, mixed>

toInteger

Function to convert array to integer values

toInteger(mixed||string|int array, int|array default = null) : mixed||string|int
static
since

1.0

Arguments

array

array<string|int, mixed>The source array to convert

default

int|array<string|int, mixed>A default value to assign if $array is not an array

Response

array<string|int, mixed>

toObject

Utility function to map an array to a stdClass object.

toObject(mixed||string|int array,  class = 'stdClass',  recursive = true) : 
static
since

1.0

Arguments

array

array<string|int, mixed>The array to map.

class

stringName of the class to create

recursive

boolConvert also any array inside the main array

Response

object

toString

Utility function to map an array to a string.

toString(mixed||string|int array,  innerGlue = '=',  outerGlue = ' ',  keepOuterKey = false) : 
static
since

1.0

Arguments

array

array<string|int, mixed>The 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

boolTrue if final key should be kept.

Response

string