class JArrayHelper

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

Methods

static  void
toInteger( array $array, mixed $default = null)

Function to convert array to integer values

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

Utility function to map an array to a stdClass object.

static  string
toString( array $array = null, string $inner_glue = '=', string $outer_glue = ' ', boolean $keepOuterKey = false)

Utility function to map an array to a string.

static  array
fromObject( object $p_obj, boolean $recurse = true, string $regex = null)

Utility function to map an object to an array

static  array
getColumn( array $array, string $index)

Extracts a column from an array of arrays or objects

static  mixed
getValue( array $array, string $name, mixed $default = null, string $type = '')

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

static  array
invert( array $array)

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

static  boolean
isAssociative( array $array)

Method to determine if an array is an associative array.

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

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

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

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

static  array
arrayUnique( array $myArray)

Multidimensional array safe unique test

Details

static void toInteger( array $array, mixed $default = null)

Function to convert array to integer values

Parameters

array $array &$array The source array to convert
mixed $default A default value (int|array) to assign if $array is not an array

Return Value

void

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

Utility function to map an array to a stdClass object.

Parameters

array $array &$array The array to map.
string $class Name of the class to create
boolean $recursive Convert also any array inside the main array

Return Value

object The object mapped from the given array

static string toString( array $array = null, string $inner_glue = '=', string $outer_glue = ' ', boolean $keepOuterKey = false)

Utility function to map an array to a string.

Parameters

array $array The array to map.
string $inner_glue The glue (optional, defaults to '=') between the key and the value.
string $outer_glue The glue (optional, defaults to ' ') between array elements.
boolean $keepOuterKey True if final key should be kept.

Return Value

string The string mapped from the given array

static array fromObject( object $p_obj, boolean $recurse = true, string $regex = null)

Utility function to map an object to an array

Parameters

object $p_obj The source object
boolean $recurse True to recurse through multi-level objects
string $regex An optional regular expression to match on field names

Return Value

array The array mapped from the given object

static array getColumn( array $array, string $index)

Extracts a column from an array of arrays or objects

Parameters

array $array &$array The source array
string $index The index of the column or name of object property

Return Value

array Column of values from the source array

static mixed getValue( array $array, string $name, mixed $default = null, string $type = '')

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

Parameters

array $array &$array A named array
string $name The key to search for
mixed $default The default value to give if no key found
string $type Return type for the variable (INT, FLOAT, STRING, WORD, BOOLEAN, ARRAY)

Return Value

mixed The value from the source array

static array invert( array $array)

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

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

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

Parameters

array $array The source array.

Return Value

array The inverted array.

static boolean isAssociative( array $array)

Method to determine if an array is an associative array.

Parameters

array $array An array to test.

Return Value

boolean True if the array is an associative array.

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

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

Parameters

array $source The source array.
string $key Where the elements of the source array are objects or arrays, the key to pivot on.

Return Value

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

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

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

Parameters

array $a &$a An array of objects
mixed $k The key (string) or an array of keys to sort on
mixed $direction Direction (integer) or an array of direction to sort in [1 = Ascending] [-1 = Descending]
mixed $caseSensitive Boolean or array of booleans to let sort occur case sensitive or insensitive
mixed $locale Boolean or array of booleans to let sort occur using the locale language or not

Return Value

array The sorted array of objects

static array arrayUnique( array $myArray)

Multidimensional array safe unique test

Parameters

array $myArray The array to make unique.

Return Value

array