neat_array.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: neat__array_8php-source.html 675 2008-12-26 00:27:14Z gwoo $ */
00003 /**
00004  * Library of array functions for Cake.
00005  *
00006  * Internal use only.
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.libs
00023  * @since           CakePHP(tm) v 0.2.9
00024  * @version         $Revision: 675 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 /**
00030  * Class used for internal manipulation of multi-dimensional arrays (arrays of arrays).
00031  *
00032  * Long description for class
00033  *
00034  * @package     cake
00035  * @subpackage  cake.cake.libs
00036  */
00037 class NeatArray{
00038 /**
00039  * Value of NeatArray.
00040  *
00041  * @var array
00042  * @access public
00043  */
00044     var $value;
00045 /**
00046  * Constructor. Defaults to an empty array.
00047  *
00048  * @param array $value
00049  * @access public
00050  * @uses NeatArray::value
00051  */
00052     function NeatArray($value = array()) {
00053         $this->value = $value;
00054     }
00055 /**
00056  * Finds and returns records with $fieldName equal to $value from this NeatArray.
00057  *
00058  * @param string $fieldName
00059  * @param string $value
00060  * @return mixed
00061  * @access public
00062  * @uses NeatArray::value
00063  */
00064     function findIn($fieldName, $value) {
00065         if (!is_array($this->value)) {
00066             return false;
00067         }
00068         $out = false;
00069         $keys = array_keys($this->value);
00070         $count = sizeof($keys);
00071 
00072         for ($i = 0; $i < $count; $i++) {
00073             if (isset($this->value[$keys[$i]][$fieldName]) && ($this->value[$keys[$i]][$fieldName] == $value))
00074             {
00075                 $out[$keys[$i]] = $this->value[$keys[$i]];
00076             }
00077         }
00078         return $out;
00079     }
00080 /**
00081  * Checks if $this->value is an array, and removes all empty elements.
00082  *
00083  * @access public
00084  * @uses NeatArray::value
00085  */
00086     function cleanup() {
00087         $out = is_array($this->value) ? array(): null;
00088         foreach ($this->value as $k => $v) {
00089             if ($v == "0") {
00090                 $out[$k] = $v;
00091             } elseif ($v) {
00092                 $out[$k] = $v;
00093             }
00094         }
00095         $this->value=$out;
00096     }
00097 /**
00098  * Adds elements from given array to itself.
00099  *
00100  * @param string $value
00101  * @return bool
00102  * @access public
00103  * @uses NeatArray::value
00104  */
00105     function add($value) {
00106         return ($this->value = $this->plus($value)) ? true : false;
00107     }
00108 /**
00109  * Returns itself merged with given array.
00110  *
00111  * @param array $value Array to add to NeatArray.
00112  * @return array
00113  * @access public
00114  * @uses NeatArray::value
00115  */
00116     function plus($value) {
00117         $merge = array_merge($this->value, (is_array($value) ? $value : array($value)));
00118         return $merge;
00119     }
00120 /**
00121  * Counts repeating strings and returns an array of totals.
00122  *
00123  * @param int $sortedBy A value of 1 sorts by values, a value of 2 sorts by keys. Defaults to null (no sorting).
00124  * @return array
00125  * @access public
00126  * @uses NeatArray::value
00127  */
00128     function totals($sortedBy = 1, $reverse = true) {
00129         $out = array();
00130         foreach ($this->value as $val) {
00131             isset($out[$val]) ? $out[$val]++ : $out[$val] = 1;
00132         }
00133 
00134         if ($sortedBy == 1) {
00135             $reverse ? arsort($out, SORT_NUMERIC) : asort($out, SORT_NUMERIC);
00136         }
00137 
00138         if ($sortedBy == 2) {
00139             $reverse ? krsort($out, SORT_STRING) : ksort($out, SORT_STRING);
00140         }
00141         return $out;
00142     }
00143 /**
00144  * Performs an array_filter() on the contents of this NeatArray.
00145  *
00146  * @param string $with Name of callback function to perform on each element of this NeatArray.
00147  * @return array
00148  */
00149     function filter($with) {
00150         return $this->value = array_filter($this->value, $with);
00151     }
00152 /**
00153  * Passes each of its values through a specified function or method.
00154  * Think of PHP's {@link http://php.net/array_walk array_walk()}.
00155  *
00156  * @param string $with Name of callback function
00157  * @return array Returns value of NeatArray::value
00158  * @access public
00159  * @uses NeatArray::value
00160  */
00161     function walk($with) {
00162         array_walk($this->value, $with);
00163         return $this->value;
00164     }
00165 /**
00166  * Apply $template to all elements of this NeatArray, and return the array itself.
00167  *
00168  * @param string $template {@link http://php.net/sprintf sprintf()}-compatible string to be applied to all values of this NeatArray.
00169  * @return array
00170  */
00171     function sprintf($template) {
00172         $count = count($this->value);
00173         for ($ii = 0; $ii < $count; $ii++) {
00174             $this->value[$ii] = sprintf($template, $this->value[$ii]);
00175         }
00176         return $this->value;
00177     }
00178 /**
00179  * Extracts a value from all array items.
00180  *
00181  * @return array
00182  * @access public
00183  * @uses NeatArray::value
00184  */
00185     function extract($name) {
00186         $out = array();
00187         foreach ($this->value as $val) {
00188             if (isset($val[$name]))
00189             $out[]=$val[$name];
00190         }
00191         return $out;
00192     }
00193 /**
00194  * Returns a list of unique elements.
00195  *
00196  * @return array
00197  */
00198     function unique() {
00199         $unique = array_unique($this->value);
00200         return $unique;
00201     }
00202 /**
00203  * Removes duplicate elements from the value and returns it.
00204  *
00205  * @return array
00206  */
00207     function makeUnique() {
00208         return $this->value = array_unique($this->value);
00209     }
00210 /**
00211  * Joins an array with myself using a key (like a join between database tables).
00212  *
00213  * Example:
00214  *
00215  * $alice = array('id'=>'1', 'name'=>'Alice');
00216  * $bob = array('id'=>'2', 'name'=>'Bob');
00217  *
00218  * $users = new NeatArray(array($alice, $bob));
00219  *
00220  * $born = array
00221  * (
00222  *    array('user_id'=>'1', 'born'=>'1980'),
00223  *    array('user_id'=>'2', 'born'=>'1976')
00224  * );
00225  *
00226  * $users->joinWith($born, 'id', 'user_id');
00227  *
00228  * Result:
00229  *
00230  * $users->value == array
00231  *    (
00232  *        array('id'=>'1', 'name'=>'Alice', 'born'=>'1980'),
00233  *        array('id'=>'2', 'name'=>'Bob',   'born'=>'1976')
00234  *    );
00235  *
00236  * @param array $his The array to join with myself.
00237  * @param string $onMine Key to use on myself.
00238  * @param string $onHis Key to use on him.
00239  * @return array
00240  */
00241     function joinWith($his, $onMine, $onHis = null) {
00242         if (empty($onHis)) {
00243             $onHis = $onMine;
00244         }
00245         $his = new NeatArray($his);
00246         $out = array();
00247 
00248         foreach ($this->value as $key => $val) {
00249             if ($fromHis = $his->findIn($onHis, $val[$onMine])) {
00250                 list($fromHis) = array_values($fromHis);
00251                 $out[$key] = array_merge($val, $fromHis);
00252             } else {
00253                 $out[$key] = $val;
00254             }
00255         }
00256         return $this->value = $out;
00257     }
00258 /**
00259  * Enter description here...
00260  * @todo Explain this function. almost looks like it creates a tree
00261  *
00262  * @param string $root
00263  * @param string $idKey
00264  * @param string $parentIdKey
00265  * @param string $childrenKey
00266  * @return array
00267  */
00268     function threaded($root = null, $idKey = 'id', $parentIdKey = 'parent_id', $childrenKey = 'children') {
00269         $out = array();
00270         $sizeof = sizeof($this->value);
00271 
00272         for ($ii = 0; $ii < $sizeof; $ii++) {
00273             if ($this->value[$ii][$parentIdKey] == $root) {
00274                 $tmp = $this->value[$ii];
00275                 $tmp[$childrenKey]=isset($this->value[$ii][$idKey])
00276                                             ? $this->threaded($this->value[$ii][$idKey], $idKey, $parentIdKey, $childrenKey) : null;
00277                 $out[] = $tmp;
00278             }
00279         }
00280         return $out;
00281     }
00282 /**
00283  * Array multi search
00284  *
00285  * @param string $search_value
00286  * @param array $the_array
00287  * @return array
00288  * @link http://php.net/array_search#47116
00289  */
00290     function multi_search($search_value, $the_array = null) {
00291         if ($the_array == null) {
00292             $the_array = $this->value;
00293         }
00294 
00295         if (is_array($the_array)) {
00296             foreach ($the_array as $key => $value) {
00297                 $result = $this->multi_search($search_value, $value);
00298 
00299                 if (is_array($result)) {
00300                     $return = $result;
00301                     array_unshift($return, $key);
00302                     return $return;
00303                 } elseif ($result == true) {
00304                     $return[]=$key;
00305                     return $return;
00306                 }
00307             }
00308             return false;
00309         } else {
00310             if ($search_value == $the_array) {
00311                 return true;
00312             } else {
00313                 return false;
00314             }
00315         }
00316     }
00317 }
00318 ?>