Source for file arrayhelper.php
Documentation is available at arrayhelper.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved
* @license GNU General Public License version 2 or later; see LICENSE
* JArrayHelper is an array utility class for doing all sorts of odds and ends with arrays.
* @package Joomla.Platform
* Option to perform case-sensitive sorts.
* @var mixed Boolean or array of booleans.
protected static $sortCase;
* Option to set the sort direction.
* @var mixed Integer or array of integers.
protected static $sortDirection;
* Option to set the object key to sort on.
protected static $sortKey;
* Option to perform a language aware sort.
* @var mixed Boolean or array of booleans.
protected static $sortLocale;
* Function to convert array to integer values
* @param array &$array The source array to convert
* @param mixed $default A default value (int|array) to assign if $array is not an array
public static function toInteger(&$array, $default =
null)
foreach ($array as $i =>
$v)
self::toInteger($default, null);
$array =
array((int)
$default);
* Utility function to map an array to a stdClass object.
* @param array &$array The array to map.
* @param string $class Name of the class to create
* @return object The object mapped from the given array
public static function toObject(&$array, $class =
'stdClass')
foreach ($array as $k =>
$v)
$obj->$k =
self::toObject($v, $class);
* Utility function to map an array to a string.
* @param array $array The array to map.
* @param string $inner_glue The glue (optional, defaults to '=') between the key and the value.
* @param string $outer_glue The glue (optional, defaults to ' ') between array elements.
* @param boolean $keepOuterKey True if final key should be kept.
* @return string The string mapped from the given array
public static function toString($array =
null, $inner_glue =
'=', $outer_glue =
' ', $keepOuterKey =
false)
foreach ($array as $key =>
$item)
// This is value is an array, go and do it again!
$output[] =
self::toString($item, $inner_glue, $outer_glue, $keepOuterKey);
$output[] =
$key .
$inner_glue .
'"' .
$item .
'"';
return implode($outer_glue, $output);
* Utility function to map an object to an array
* @param object $p_obj The source object
* @param boolean $recurse True to recurse through multi-level objects
* @param string $regex An optional regular expression to match on field names
* @return array The array mapped from the given object
public static function fromObject($p_obj, $recurse =
true, $regex =
null)
return self::_fromObject($p_obj, $recurse, $regex);
* Utility function to map an object or array to an array
* @param mixed $item The source object or array
* @param boolean $recurse True to recurse through multi-level objects
* @param string $regex An optional regular expression to match on field names
* @return array The array mapped from the given object
protected static function _fromObject($item, $recurse, $regex)
$result[$k] =
self::_fromObject($v, $recurse, $regex);
foreach ($item as $k =>
$v)
$result[$k] =
self::_fromObject($v, $recurse, $regex);
* Extracts a column from an array of arrays or objects
* @param array &$array The source array
* @param string $index The index of the column or name of object property
* @return array Column of values from the source array
public static function getColumn(&$array, $index)
foreach ($array as &$item)
if (is_array($item) && isset
($item[$index]))
$result[] =
$item[$index];
elseif (is_object($item) && isset
($item->$index))
$result[] =
$item->$index;
* Utility function to return a value from a named array or a specified default
* @param array &$array A named array
* @param string $name The key to search for
* @param mixed $default The default value to give if no key found
* @param string $type Return type for the variable (INT, FLOAT, STRING, WORD, BOOLEAN, ARRAY)
* @return mixed The value from the source array
public static function getValue(&$array, $name, $default =
null, $type =
'')
if (isset
($array[$name]))
// Handle the default case
// Handle the type constraint
// Only use the first integer value
$result =
@(int)
$matches[0];
// Only use the first floating point value
@preg_match('/-?[0-9]+(\.[0-9]+)?/', $result, $matches);
$result =
@(float)
$matches[0];
$result = (bool)
$result;
$result =
array($result);
$result = (string)
$result;
* Takes an associative array of arrays and inverts the array keys to values using the array values as keys.
* 'New' => array('1000', '1500', '1750'),
* 'Used' => array('3000', '4000', '5000', '6000')
* $output = JArrayHelper::invert($input);
* Output would be equal to:
* @param array $array The source array.
* @return array The inverted array.
public static function invert($array)
foreach ($array as $base =>
$values)
foreach ($values as $key)
// If the key isn't scalar then ignore it.
* Method to determine if an array is an associative array.
* @param array $array An array to test.
* @return boolean True if the array is an associative array.
* Pivots an array to create a reverse lookup of an array of scalars, arrays or objects.
* @param array $source The source array.
* @param string $key Where the elements of the source array are objects or arrays, the key to pivot on.
* @return array An array of arrays pivoted either on the value of the keys, or an individual key of an object or array.
public static function pivot($source, $key =
null)
foreach ($source as $index =>
$value)
// Determine the name of the pivot key, and its value.
// If the key does not exist, ignore it.
if (!isset
($value[$key]))
$resultKey =
$value[$key];
$resultValue =
&$source[$index];
// If the key does not exist, ignore it.
if (!isset
($value->$key))
$resultKey =
$value->$key;
$resultValue =
&$source[$index];
// The counter tracks how many times a key has been used.
if (empty($counter[$resultKey]))
// The first time around we just assign the value to the key.
$result[$resultKey] =
$resultValue;
$counter[$resultKey] =
1;
elseif ($counter[$resultKey] ==
1)
// If there is a second time, we convert the value into an array.
$result[$resultKey] =
array(
// After the second time, no need to track any more. Just append to the existing array.
$result[$resultKey][] =
$resultValue;
* Utility function to sort an array of objects on a given field
* @param array &$a An array of objects
* @param mixed $k The key (string) or a array of key to sort on
* @param mixed $direction Direction (integer) or an array of direction to sort in [1 = Ascending] [-1 = Descending]
* @param mixed $caseSensitive Boolean or array of booleans to let sort occur case sensitive or insensitive
* @param mixed $locale Boolean or array of booleans to let sort occur using the locale language or not
* @return array The sorted array of objects
public static function sortObjects(&$a, $k, $direction =
1, $caseSensitive =
true, $locale =
false)
$locale =
array($locale);
self::$sortCase = (array)
$caseSensitive;
self::$sortDirection = (array)
$direction;
self::$sortKey = (array)
$k;
self::$sortLocale =
$locale;
usort($a, array(__CLASS__
, '_sortObjects'));
self::$sortDirection =
null;
self::$sortLocale =
null;
* Callback function for sorting an array of objects on a key
* @param array &$a An array of objects
* @param array &$b An array of objects
* @return integer Comparison status
* @see JArrayHelper::sortObjects()
for ($i =
0, $count =
count($key); $i <
$count; $i++
)
if (isset
(self::$sortDirection[$i]))
$direction =
self::$sortDirection[$i];
if (isset
(self::$sortCase[$i]))
$caseSensitive =
self::$sortCase[$i];
if (isset
(self::$sortLocale[$i]))
$locale =
self::$sortLocale[$i];
* Multidimensional array safe unique test
* @param array $myArray The array to make unique.
* @see http://php.net/manual/en/function.array-unique.php
foreach ($myArray as &$myvalue)
foreach ($myArray as &$myvalue)
Documentation generated on Tue, 19 Nov 2013 14:53:51 +0100 by phpDocumentor 1.4.3