[ Index ] |
|
Code source de CakePHP 1.1.13.4450 |
1 <?php 2 /* SVN FILE: $Id: neat_array.php 4409 2007-02-02 13:20:59Z phpnut $ */ 3 /** 4 * Library of array functions for Cake. 5 * 6 * Internal use only. 7 * 8 * PHP versions 4 and 5 9 * 10 * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> 11 * Copyright 2005-2007, Cake Software Foundation, Inc. 12 * 1785 E. Sahara Avenue, Suite 490-204 13 * Las Vegas, Nevada 89104 14 * 15 * Licensed under The MIT License 16 * Redistributions of files must retain the above copyright notice. 17 * 18 * @filesource 19 * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. 20 * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project 21 * @package cake 22 * @subpackage cake.cake.libs 23 * @since CakePHP(tm) v 0.2.9 24 * @version $Revision: 4409 $ 25 * @modifiedby $LastChangedBy: phpnut $ 26 * @lastmodified $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $ 27 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 28 */ 29 /** 30 * Class used for internal manipulation of multi-dimensional arrays (arrays of arrays). 31 * 32 * Long description for class 33 * 34 * @package cake 35 * @subpackage cake.cake.libs 36 */ 37 class NeatArray{ 38 /** 39 * Value of NeatArray. 40 * 41 * @var array 42 * @access public 43 */ 44 var $value; 45 /** 46 * Constructor. Defaults to an empty array. 47 * 48 * @param array $value 49 * @access public 50 * @uses NeatArray::value 51 */ 52 function NeatArray($value = array()) { 53 $this->value = $value; 54 } 55 /** 56 * Finds and returns records with $fieldName equal to $value from this NeatArray. 57 * 58 * @param string $fieldName 59 * @param string $value 60 * @return mixed 61 * @access public 62 * @uses NeatArray::value 63 */ 64 function findIn($fieldName, $value) { 65 if (!is_array($this->value)) { 66 return false; 67 } 68 $out = false; 69 $keys = array_keys($this->value); 70 $count = sizeof($keys); 71 72 for($i = 0; $i < $count; $i++) { 73 if (isset($this->value[$keys[$i]][$fieldName]) && ($this->value[$keys[$i]][$fieldName] == $value)) 74 { 75 $out[$keys[$i]] = $this->value[$keys[$i]]; 76 } 77 } 78 return $out; 79 } 80 /** 81 * Checks if $this->value is an array, and removes all empty elements. 82 * 83 * @access public 84 * @uses NeatArray::value 85 */ 86 function cleanup() { 87 $out = is_array($this->value) ? array(): null; 88 foreach($this->value as $k => $v) { 89 if ($v == "0") { 90 $out[$k] = $v; 91 } elseif ($v) { 92 $out[$k] = $v; 93 } 94 } 95 $this->value=$out; 96 } 97 /** 98 * Adds elements from given array to itself. 99 * 100 * @param string $value 101 * @return bool 102 * @access public 103 * @uses NeatArray::value 104 */ 105 function add($value) { 106 return ($this->value = $this->plus($value)) ? true : false; 107 } 108 /** 109 * Returns itself merged with given array. 110 * 111 * @param array $value Array to add to NeatArray. 112 * @return array 113 * @access public 114 * @uses NeatArray::value 115 */ 116 function plus($value) { 117 $merge = array_merge($this->value, (is_array($value) ? $value : array($value))); 118 return $merge; 119 } 120 /** 121 * Counts repeating strings and returns an array of totals. 122 * 123 * @param int $sortedBy A value of 1 sorts by values, a value of 2 sorts by keys. Defaults to null (no sorting). 124 * @return array 125 * @access public 126 * @uses NeatArray::value 127 */ 128 function totals($sortedBy = 1, $reverse = true) { 129 $out = array(); 130 foreach($this->value as $val) { 131 isset($out[$val]) ? $out[$val]++ : $out[$val] = 1; 132 } 133 134 if ($sortedBy == 1) { 135 $reverse ? arsort($out, SORT_NUMERIC) : asort($out, SORT_NUMERIC); 136 } 137 138 if ($sortedBy == 2) { 139 $reverse ? krsort($out, SORT_STRING) : ksort($out, SORT_STRING); 140 } 141 return $out; 142 } 143 /** 144 * Performs an array_filter() on the contents of this NeatArray. 145 * 146 * @param string $with Name of callback function to perform on each element of this NeatArray. 147 * @return array 148 */ 149 function filter($with) { 150 return $this->value = array_filter($this->value, $with); 151 } 152 /** 153 * Passes each of its values through a specified function or method. 154 * Think of PHP's {@link http://php.net/array_walk array_walk()}. 155 * 156 * @param string $with Name of callback function 157 * @return array Returns value of NeatArray::value 158 * @access public 159 * @uses NeatArray::value 160 */ 161 function walk($with) { 162 array_walk($this->value, $with); 163 return $this->value; 164 } 165 /** 166 * Apply $template to all elements of this NeatArray, and return the array itself. 167 * 168 * @param string $template {@link http://php.net/sprintf sprintf()}-compatible string to be applied to all values of this NeatArray. 169 * @return array 170 */ 171 function sprintf($template) { 172 $count = count($this->value); 173 for($ii = 0; $ii < $count; $ii++) { 174 $this->value[$ii] = sprintf($template, $this->value[$ii]); 175 } 176 return $this->value; 177 } 178 /** 179 * Extracts a value from all array items. 180 * 181 * @return array 182 * @access public 183 * @uses NeatArray::value 184 */ 185 function extract($name) { 186 $out = array(); 187 foreach($this->value as $val) { 188 if (isset($val[$name])) 189 $out[]=$val[$name]; 190 } 191 return $out; 192 } 193 /** 194 * Returns a list of unique elements. 195 * 196 * @return array 197 */ 198 function unique() { 199 $unique = array_unique($this->value); 200 return $unique; 201 } 202 /** 203 * Removes duplicate elements from the value and returns it. 204 * 205 * @return array 206 */ 207 function makeUnique() { 208 return $this->value = array_unique($this->value); 209 } 210 /** 211 * Joins an array with myself using a key (like a join between database tables). 212 * 213 * Example: 214 * 215 * $alice = array('id'=>'1', 'name'=>'Alice'); 216 * $bob = array('id'=>'2', 'name'=>'Bob'); 217 * 218 * $users = new NeatArray(array($alice, $bob)); 219 * 220 * $born = array 221 * ( 222 * array('user_id'=>'1', 'born'=>'1980'), 223 * array('user_id'=>'2', 'born'=>'1976') 224 * ); 225 * 226 * $users->joinWith($born, 'id', 'user_id'); 227 * 228 * Result: 229 * 230 * $users->value == array 231 * ( 232 * array('id'=>'1', 'name'=>'Alice', 'born'=>'1980'), 233 * array('id'=>'2', 'name'=>'Bob', 'born'=>'1976') 234 * ); 235 * 236 * @param array $his The array to join with myself. 237 * @param string $onMine Key to use on myself. 238 * @param string $onHis Key to use on him. 239 * @return array 240 */ 241 function joinWith($his, $onMine, $onHis = null) { 242 if (empty($onHis)) { 243 $onHis = $onMine; 244 } 245 $his = new NeatArray($his); 246 $out = array(); 247 248 foreach($this->value as $key => $val) { 249 if ($fromHis = $his->findIn($onHis, $val[$onMine])) { 250 list($fromHis) = array_values($fromHis); 251 $out[$key] = array_merge($val, $fromHis); 252 } else { 253 $out[$key] = $val; 254 } 255 } 256 return $this->value = $out; 257 } 258 /** 259 * Enter description here... 260 * @todo Explain this function. almost looks like it creates a tree 261 * 262 * @param string $root 263 * @param string $idKey 264 * @param string $parentIdKey 265 * @param string $childrenKey 266 * @return array 267 */ 268 function threaded($root = null, $idKey = 'id', $parentIdKey = 'parent_id', $childrenKey = 'children') { 269 $out = array(); 270 $sizeof = sizeof($this->value); 271 272 for($ii = 0; $ii < $sizeof; $ii++) { 273 if ($this->value[$ii][$parentIdKey] == $root) { 274 $tmp = $this->value[$ii]; 275 $tmp[$childrenKey]=isset($this->value[$ii][$idKey]) 276 ? $this->threaded($this->value[$ii][$idKey], $idKey, $parentIdKey, $childrenKey) : null; 277 $out[] = $tmp; 278 } 279 } 280 return $out; 281 } 282 /** 283 * Array multi search 284 * 285 * @param string $search_value 286 * @param array $the_array 287 * @return array 288 * @link http://php.net/array_search#47116 289 */ 290 function multi_search($search_value, $the_array = null) { 291 if ($the_array == null) { 292 $the_array = $this->value; 293 } 294 295 if (is_array($the_array)) { 296 foreach($the_array as $key => $value) { 297 $result = $this->multi_search($search_value, $value); 298 299 if (is_array($result)) { 300 $return = $result; 301 array_unshift($return, $key); 302 return $return; 303 } elseif ($result == true) { 304 $return[]=$key; 305 return $return; 306 } 307 } 308 return false; 309 } else { 310 if ($search_value == $the_array) { 311 return true; 312 } else { 313 return false; 314 } 315 } 316 } 317 } 318 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 19:27:47 2007 | par Balluche grâce à PHPXref 0.7 |