10 defined(
'JPATH_PLATFORM') or die;
63 public static function toInteger(&$array, $default = null)
67 foreach ($array as $i => $v)
69 $array[$i] = (int) $v;
74 if ($default === null)
78 elseif (is_array($default))
80 self::toInteger($default, null);
85 $array = array((
int) $default);
100 public static function toObject(&$array, $class =
'stdClass')
104 if (is_array($array))
108 foreach ($array as $k => $v)
112 $obj->$k = self::toObject($v, $class);
135 public static function toString($array = null, $inner_glue =
'=', $outer_glue =
' ', $keepOuterKey =
false)
139 if (is_array($array))
141 foreach ($array as $key => $item)
150 $output[] = self::toString($item, $inner_glue, $outer_glue, $keepOuterKey);
154 $output[] = $key . $inner_glue .
'"' . $item .
'"';
159 return implode($outer_glue, $output);
173 public static function fromObject($p_obj, $recurse =
true, $regex = null)
175 if (is_object($p_obj))
177 return self::_fromObject($p_obj, $recurse, $regex);
196 protected static function _fromObject($item, $recurse, $regex)
198 if (is_object($item))
202 foreach (get_object_vars($item) as $k => $v)
204 if (!$regex || preg_match($regex, $k))
208 $result[$k] = self::_fromObject($v, $recurse, $regex);
217 elseif (is_array($item))
221 foreach ($item as $k => $v)
223 $result[$k] = self::_fromObject($v, $recurse, $regex);
243 public static function getColumn(&$array, $index)
247 if (is_array($array))
249 foreach ($array as &$item)
251 if (is_array($item) && isset($item[$index]))
253 $result[] = $item[$index];
255 elseif (is_object($item) && isset($item->$index))
257 $result[] = $item->$index;
277 public static function getValue(&$array, $name, $default = null, $type =
'')
281 if (isset($array[$name]))
283 $result = $array[$name];
287 if (is_null($result))
293 switch (strtoupper($type))
298 @preg_match(
'/-?[0-9]+/', $result, $matches);
299 $result = @(int) $matches[0];
305 @preg_match(
'/-?[0-9]+(\.[0-9]+)?/', $result, $matches);
306 $result = @(float) $matches[0];
311 $result = (bool) $result;
315 if (!is_array($result))
317 $result = array($result);
322 $result = (string) $result;
326 $result = (string) preg_replace(
'#\W#',
'', $result);
364 public static function invert($array)
368 foreach ($array as $base => $values)
370 if (!is_array($values))
375 foreach ($values as $key)
380 $return[$key] = $base;
396 public static function isAssociative($array)
398 if (is_array($array))
400 foreach (array_keys($array) as $k => $v)
422 public static function pivot($source, $key = null)
427 foreach ($source as $index => $value)
430 if (is_array($value))
433 if (!isset($value[$key]))
438 $resultKey = $value[$key];
439 $resultValue = &$source[$index];
441 elseif (is_object($value))
444 if (!isset($value->$key))
449 $resultKey = $value->$key;
450 $resultValue = &$source[$index];
456 $resultValue = $index;
460 if (empty($counter[$resultKey]))
463 $result[$resultKey] = $resultValue;
464 $counter[$resultKey] = 1;
466 elseif ($counter[$resultKey] == 1)
469 $result[$resultKey] = array(
473 $counter[$resultKey]++;
478 $result[$resultKey][] = $resultValue;
500 public static function sortObjects(&$a, $k, $direction = 1, $caseSensitive =
true, $locale =
false)
502 if (!is_array($locale) || !is_array($locale[0]))
504 $locale = array($locale);
507 self::$sortCase = (array) $caseSensitive;
508 self::$sortDirection = (array) $direction;
509 self::$sortKey = (array) $k;
510 self::$sortLocale = $locale;
512 usort($a, array(__CLASS__,
'_sortObjects'));
514 self::$sortCase = null;
515 self::$sortDirection = null;
516 self::$sortKey = null;
517 self::$sortLocale = null;
533 protected static function _sortObjects(&$a, &$b)
535 $key = self::$sortKey;
537 for ($i = 0, $count = count($key); $i < $count; $i++)
539 if (isset(self::$sortDirection[$i]))
541 $direction = self::$sortDirection[$i];
544 if (isset(self::$sortCase[$i]))
546 $caseSensitive = self::$sortCase[$i];
549 if (isset(self::$sortLocale[$i]))
551 $locale = self::$sortLocale[$i];
557 if ((is_bool($va) || is_numeric($va)) && (is_bool($vb) || is_numeric($vb)))
561 elseif ($caseSensitive)
595 public static function arrayUnique($myArray)
597 if (!is_array($myArray))
602 foreach ($myArray as &$myvalue)
604 $myvalue = serialize($myvalue);
607 $myArray = array_unique($myArray);
609 foreach ($myArray as &$myvalue)
611 $myvalue = unserialize($myvalue);