1: <?php
2: /**
3: * This file contains the array utility class.
4: *
5: * @package Core
6: * @subpackage Util
7: * @version SVN Revision $Rev:$
8: *
9: * @author Murat Purc <murat@purc.de>
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: /**
19: * Array helper class.
20: *
21: * @package Core
22: * @subpackage Util
23: */
24: class cArray {
25:
26: /**
27: * Strip whitespace (or other characters) from the beginning and end of each item in array.
28: * Similar to trim() function.
29: * @param array $arr
30: * @param string $charlist
31: * @return array The trimmer array
32: */
33: public static function trim(array $arr, $charlist = null) {
34: foreach ($arr as $key => $value) {
35: $arr[$key] = trim($value, $charlist);
36: }
37:
38: return $arr;
39: }
40:
41: /**
42: * @TODO: Ask timo to document this.
43: *
44: * Note: If subarrays exists, this function currently returns the key of the array
45: * given by $arr, and not from the subarrays (todo: add flag to allow this)
46: *
47: * @param array $arr The array to search
48: * @param mixed $search The value to search in the array
49: * @param bool $partial
50: * @param bool $strict
51: * @return mixed|bool The key/index of the array containing the searched value or false.
52: */
53: public static function searchRecursive(array $arr, $search, $partial = false, $strict = false) {
54: foreach ($arr as $key => $value) {
55: if (is_array($value)) {
56: $val = self::searchRecursive($value, $search, $partial, $strict);
57: if ($val !== false) {
58: return $key;
59: }
60: } else {
61: if ($partial == false) {
62: if ($strict == true) {
63: if ($value === $search) {
64: return $key;
65: }
66: } else {
67: if ($value == $search) {
68: return $key;
69: }
70: }
71: } else {
72: if (strpos($value, $search) !== false) {
73: return $key;
74: }
75: }
76: }
77: }
78:
79: return false;
80: }
81:
82: /**
83: * Sorts an array by changing the locale temporary to passed value.
84: *
85: * @param array $arr The array to sort
86: * @param string $locale The locale to change before sorting
87: * @return array Sorted array
88: */
89: public static function sortWithLocale(array $arr, $locale) {
90: $oldlocale = setlocale(LC_COLLATE, 0);
91: setlocale(LC_COLLATE, $locale);
92:
93: uasort($arr, 'strcoll');
94:
95: setlocale(LC_COLLATE, $oldlocale);
96:
97: return $arr;
98: }
99:
100: /**
101: * Very cool algorithm for sorting multi-dimensional arrays. Found at http://us2.php.net/manual/en/function.array-multisort.php
102: * Syntax:
103: * <pre>
104: * $new_array = cArray::csort($array [, 'col1' [, SORT_FLAG [, SORT_FLAG]]]...);
105: * </pre>
106: * Explanation: $array is the array you want to sort, 'col1' is the name of the column
107: * you want to sort, SORT_FLAGS are : SORT_ASC, SORT_DESC, SORT_REGULAR, SORT_NUMERIC, SORT_STRING
108: * you can repeat the 'col',FLAG,FLAG, as often you want, the highest prioritiy is given to
109: * the first - so the array is sorted by the last given column first, then the one before ...
110: * Example:
111: * <pre>
112: * $array = cArray::csort($array,'town','age', SORT_DESC, 'name');
113: * </pre>
114: *
115: * @param multiple
116: * @return array
117: */
118: public static function csort() {
119: $args = func_get_args();
120: $marray = array_shift($args);
121: $msortline = "return(array_multisort(";
122: $i = 0;
123: foreach ($args as $arg) {
124: $i++;
125: if (is_string($arg)) {
126: foreach ($marray as $row) {
127: $a = strtoupper($row[$arg]);
128: $sortarr[$i][] = $a;
129: }
130: } else {
131: $sortarr[$i] = $arg;
132: }
133: $msortline .= "\$sortarr[" . $i . "],";
134: }
135: $msortline .= "\$marray));";
136: @eval($msortline);
137: return $marray;
138: }
139:
140: /**
141: * Ensures that the passed array has the key, sets it by using te value
142: * @param array $aArray
143: * @param string $sKey
144: * @param mixed $mDefault
145: */
146: public static function initializeKey(&$aArray, $sKey, $mDefault = '') {
147: if (!is_array($aArray)) {
148: if (isset($aArray)) {
149: return false;
150: }
151: $aArray = array();
152: }
153:
154: if (!array_key_exists($sKey, $aArray)) {
155: $aArray[$sKey] = $mDefault;
156: }
157: }
158:
159: }
160:
161: