【php开源代码栏目提醒】:网学会员php开源代码为您提供neat_array.php参考,解决您在neat_array.php学习中工作中的难题,参考学习。
<?php
/* SVN FILE: $Id: neat_array.php 4409 2007-02-02 13:20:59Z phpnut $ */
/**
* Library of array functions for Cake.
*
* Internal use only.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
* Copyright 2005-2007, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp Cake
PHP(tm) Project
* @package cake
* @subpackage cake.cake.libs
* @since CakePHP(tm) v 0.2.9
* @version $Revision: 4409 $
* @modifiedby $LastChangedBy: phpnut $
* @lastmodified $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Class used for internal manipulation of multi-dimensional arrays (arrays of arrays).
*
* Long description for class
*
* @package cake
* @subpackage cake.cake.libs
*/
class NeatArray{
/**
* Value of NeatArray.
*
* @var array
* @access public
*/
var $value;
/**
* Constructor. Defaults to an empty array.
*
* @param array $value
* @access public
* @uses NeatArray::value
*/
function NeatArray($value = array()) {
$this->value = $value;
}
/**
* Finds and returns records with $fieldName equal to $value from this NeatArray.
*
* @param string $fieldName
* @param string $value
* @return mixed
* @access public
* @uses NeatArray::value
*/
function findIn($fieldName, $value) {
if (!is_array($this->value)) {
return false;
}
$out = false;
$keys = array_keys($this->value);
$count = sizeof($keys);
for($i = 0; $i < $count; $i++) {
if (isset($this->value[$keys[$i]][$fieldName]) && ($this->value[$keys[$i]][$fieldName] == $value))
{
$out[$keys[$i]] = $this->value[$keys[$i]];
}
}
return $out;
}
/**
* Checks if $this->value is an array, and removes all empty elements.
*
* @access public
* @uses NeatArray::value
*/
function cleanup() {
$out = is_array($this->value) ? array(): null;
foreach($this->value as $k => $v) {
if ($v == "0") {
$out[$k] = $v;
} elseif ($v) {
$out[$k] = $v;
}
}
$this->value=$out;
}
/**
* Adds elements from given array to itself.
*
* @param string $value
* @return bool
* @access public
* @uses NeatArray::value
*/
function add($value) {
return ($this->value = $this->plus($value)) ? true : false;
}
/**
* Returns itself merged with given array.
*
* @param array $value Array to add to NeatArray.
* @return array
* @access public
* @uses NeatArray::value
*/
function plus($value) {
$merge = array_merge($this->value, (is_array($value) ? $value : array($value)));
return $merge;
}
/**
* Counts repeating strings and returns an array of totals.
*
* @param int $sortedBy A value of 1 sorts by values, a value of 2 sorts by keys. Defaults to null (no sorting).
* @return array
* @access public
* @uses NeatArray::value
*/
function totals($sortedBy = 1, $reverse = true) {
$out = array();
foreach($this->value as $val) {
isset($out[$val]) ? $out[$val]++ : $out[$val] = 1;
}
if ($sortedBy == 1) {
$reverse ? arsort($out, SORT_NUMERIC) : asort($out, SORT_NUMERIC);
}
if ($sortedBy == 2) {
$reverse ? krsort($out, SORT_STRING) : ksort($out, SORT_STRING);
}
return $out;
}
/**
* Performs an array_filter() on the contents of this NeatArray.
*
* @param string $with Name of callback function to perform on each element of this NeatArray.
* @return array
*/
function filter($with) {
return $this->value = array_filter($this->value, $with);
}
/**
* Passes each of its values through a specified function or method.
* Think of PHP's {@link http://php.net/array_walk array_walk()}.
*
* @param string $with Name of callback function
* @return array Returns value of NeatArray::value
* @access public
* @uses NeatArray::value
*/
function walk($with) {
array_walk($this->value, $with);
return $this->value;
}
/**
* Apply $template to all elements of this NeatArray, and return the array itself.
*
* @param string $template {@link http://php.net/sprintf sprintf()}-compatible string to be applied to all values of this NeatArray.
* @return array
*/
function sprintf($template) {
$count = count($this->value);
for($ii = 0; $ii < $count; $ii++) {
$this->value[$ii] = sprintf($template, $this->value[$ii]);
}
return $this->value;
}
/**
* Extracts a valu