Source code for file /joomla/utilities/array.php
Documentation is available at array.php
* @version $Id: array.php 6150 2007-01-02 04:53:35Z Jinx $
* @package Joomla.Framework
* @copyright Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant to the
* GNU General Public License, and as distributed it includes or is derivative
* of works licensed under the GNU General Public License or other free or open
* source software licenses. See COPYRIGHT.php for copyright notices and
* JArrayHelper is an array utility class for doing all sorts of odds and ends with arrays.
* @package Joomla.Framework
* Function to convert array to integer values
* @param array $array The source array to convert
* @param int $default A default value to assign if $array is not an array
function toInteger( &$array, $default =
null)
foreach ($array as $i =>
$v) {
* Utility function to map an array to a stdClass object.
* @param array $array The array to map.
* @param boolean $jobject Optionally create a JObject
* @return object The object mapped from the given array
function toObject(&$array, $toJObject =
false)
$obj =
$toJObject ?
new JObject() :
new stdClass();
foreach ($array as $k =>
$v)
* Utility function to map an object to an array
* @param object The source object
* @param boolean True to recurve through multi-level objects
* @param string An optional regular expression to match on field names
* @return array The array mapped from the given object
function fromObject( $p_obj, $recurse =
true, $regex =
null )
* 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
for ($i =
0; $i <
$n; $i++
)
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
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;
* Utility function to sort an array of objects on a given field
* @param array $arr An array of objects
* @param string $k The key to sort on
* @param int $direction Direction to sort in [1 = Ascending] [-1 = Descending]
* @return array The sorted array of objects
$GLOBALS['JAH_so'] =
array(
'direction' =>
$direction
usort( $a, array('JArrayHelper', '_sortObjects') );
unset
( $GLOBALS['JAH_so'] );
* Private 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 int Comparison status
* @see JArrayHelper::sortObjects()
$params =
$GLOBALS['JAH_so'];
if ( $a->$params['key'] >
$b->$params['key'] ) {
return $params['direction'];
if ( $a->$params['key'] <
$b->$params['key'] ) {
return -
1 *
$params['direction'];