[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * The Horde_Array:: class provides various methods for array manipulation. 4 * 5 * $Horde: framework/Util/Array.php,v 1.26.2.8 2006/01/01 21:28:41 jan Exp $ 6 * 7 * Copyright 2003-2006 Michael Slusarz <slusarz@bigworm.colorado.edu> 8 * Copyright 2003-2006 Marko Djukic <marko@oblo.com> 9 * Copyright 2003-2006 Jan Schneider <jan@horde.org> 10 * 11 * See the enclosed file COPYING for license information (LGPL). If you 12 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 13 * 14 * @author Michael Slusarz <slusarz@bigworm.colorado.edu> 15 * @author Marko Djukic <marko@oblo.com> 16 * @author Jan Schneider <jan@horde.org> 17 * @since Horde 3.0 18 * @package Horde_Util 19 */ 20 class Horde_Array { 21 22 /** 23 * Prepare a list of addresses for storage. 24 * Namely, trims and lowercases all addresses and then sort. 25 * 26 * @param array $addr The list of addresses. 27 * 28 * @return array The list of addresses, prepared for storage. 29 */ 30 function prepareAddressList($addr) 31 { 32 /* Remove any extra space in the address and make it lowercase. */ 33 $addr = array_map('trim', $addr); 34 $addr = array_map(array('String', 'lower'), $addr); 35 36 /* Remove duplicate entries. */ 37 $addr = array_unique($addr); 38 39 /* Sort the list. */ 40 usort($addr, array('Horde_Array', 'sortAddressList')); 41 42 return $addr; 43 } 44 45 /** 46 * Function used by usort() to sort an address list. 47 * e.g. usort($foo, array('Horde_Array', 'sortAddressList')); 48 * 49 * @param string $a Address #1. 50 * @param string $b Address #2. 51 * 52 * @return integer -1, 0, or 1. 53 */ 54 function sortAddressList($a, $b) 55 { 56 $a = explode('@', $a); 57 $b = explode('@', $b); 58 59 /* One of the addresses doesn't have a host name. */ 60 if (empty($a[0])) { 61 array_shift($a); 62 } 63 if (empty($b[0])) { 64 array_shift($b); 65 } 66 if (count($a) != count($b)) { 67 return (count($a) > count($b)); 68 } 69 70 /* The addresses have different hostname or not hostname and 71 different mailbox names. */ 72 if ($a[(count($a) - 1)] != $b[(count($b) - 1)]) { 73 return strcmp($a[(count($a) - 1)], $b[(count($b) - 1)]); 74 } 75 76 /* Compare mailbox names. */ 77 return strcmp($a[0], $b[0]); 78 } 79 80 /** 81 * Sorts an array on a specified key. If the key does not exist, 82 * defaults to the first key of the array. 83 * 84 * @param array &$array The array to be sorted, passed by reference. 85 * @param string $key The key by which to sort. If not specified 86 * then the first key is used. 87 * @param integer $direction Sort direction 88 * 0 = ascending (default) 89 * 1 = descending 90 * @param boolean $associate Keep key value association? 91 */ 92 function arraySort(&$array, $key = null, $direction = 0, $associate = true) 93 { 94 /* Return if the array is empty. */ 95 if (empty($array)) { 96 return; 97 } 98 99 /* If no key to sort by is specified, use the first key. */ 100 if (is_null($key)) { 101 $keys = array_keys(each($array)); 102 $key = $keys[0]; 103 } 104 105 /* Create the function that will be used to sort the keys. */ 106 if ($direction) { 107 $function = sprintf('return strcoll(String::lower($b[\'%1$s\'], true), String::lower($a[\'%1$s\'], true));', $key); 108 } else { 109 $function = sprintf('return strcoll(String::lower($a[\'%1$s\'], true), String::lower($b[\'%1$s\'], true));', $key); 110 } 111 112 /* Call the appropriate sort function. */ 113 if ($associate) { 114 uasort($array, create_function('$a, $b', $function)); 115 } else { 116 usort($array, create_function('$a, $b', $function)); 117 } 118 } 119 120 /** 121 * Given an HTML type array field "example[key1][key2][key3]" breaks up 122 * the keys so that they could be used to reference a regular PHP array. 123 * 124 * @param string $field The field name to be examined. 125 * @param string &$base Set to the base element. 126 * @param array &$keys Set to the list of keys. 127 * 128 * @return boolean True on sucess, false on error. 129 */ 130 function getArrayParts($field, &$base, &$keys) 131 { 132 if (preg_match('|([^\[]*)((\[[^\[\]]*\])+)|', $field, $matches)) { 133 $base = $matches[1]; 134 $keys = explode('][', $matches[2]); 135 $keys[0] = substr($keys[0], 1); 136 $keys[count($keys) - 1] = substr($keys[count($keys) - 1], 0, strlen($keys[count($keys) - 1]) - 1); 137 return true; 138 } else { 139 return false; 140 } 141 } 142 143 /** 144 * Using an array of keys itarate through the array following the 145 * keys to find the final key value. If a value is passed then set 146 * that value. 147 * 148 * @param array &$array The array to be used. 149 * @param array &$keys The key path to follow as an array. 150 * @param array $value If set the target element will have this value set 151 * to it. 152 * 153 * @return mixed The final value of the key path. 154 */ 155 function getElement(&$array, &$keys, $value = null) 156 { 157 if (count($keys)) { 158 $key = array_shift($keys); 159 if (isset($array[$key])) { 160 return Horde_Array::getElement($array[$key], $keys, $value); 161 } else { 162 return false; 163 } 164 } else { 165 if (!is_null($value)) { 166 $array = $value; 167 } 168 return $array; 169 } 170 } 171 172 /** 173 * Returns a rectangle of a two-dimensional array. 174 * 175 * @param array &$array The array to extract the rectangle from. 176 * @param integer $row The start row of the rectangle. 177 * @param integer $col The start column of the rectangle. 178 * @param integer $height The height of the rectangle. 179 * @param integer $width The width of the rectangle. 180 * 181 * @return array The extracted rectangle. 182 */ 183 function &getRectangle(&$array, $row, $col, $height, $width) 184 { 185 $rec = array(); 186 for ($y = $row; $y < $row + $height; $y++) { 187 $rec[] = array_slice($array[$y], $col, $width); 188 } 189 return $rec; 190 } 191 192 /** 193 * Given an array, returns an associative array with each element key 194 * derived from its value. 195 * For example: 196 * array(0 => 'foo', 1 => 'bar') 197 * would become: 198 * array('foo' => 'foo', 'bar' => 'bar') 199 * 200 * @param array $array An array of values. 201 * 202 * @return array An array with keys the same as values. 203 */ 204 function valuesToKeys($array) 205 { 206 $mapped = array(); 207 foreach ($array as $value) { 208 $mapped[$value] = $value; 209 } 210 return $mapped; 211 } 212 213 /** 214 * Creates an array by using one array for keys and another for 215 * its values. Only exists in PHP5, so we call array_combine if it 216 * exists and otherwise emulate it. 217 * 218 * @param array $keys Key array. 219 * @param array $values Value array. 220 * 221 * @return mixed False if there are no elements, or the combined array. 222 */ 223 function combine($keys, $values) 224 { 225 if (function_exists('array_combine')) { 226 return array_combine($keys, $values); 227 } 228 229 $size = count($keys); 230 if (($size != count($values)) || ($size == 0)) { 231 return false; 232 } 233 234 for ($x = 0; $x < $size; $x++) { 235 $array[$keys[$x]] = $values[$x]; 236 } 237 return $array; 238 } 239 240 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |