Array.class.php 1.7 KB
<?php
/**
 * 数组操作类
 * 
 * @author xiaoma 2012.3.19
 * 
 *
 */
class Util_Utils_Array {
	
	/**
	 * 对二维数组进行排序 
	 * @example sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2"……)
	 * @author  xiaoma
	 * @param  array   $ArrayData   the array to sort.
	 * @param  string  $KeyName1    the first item to sort by.
	 * @param  string  $SortOrder1  the order to sort by("SORT_ASC"|"SORT_DESC")
	 * @param  string  $SortType1   the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING")
	 * @return array                sorted array.
	 */
	static function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR")
	{
		if(!is_array($ArrayData))
		{
			return $ArrayData;
		}
		// Get args number.
		$ArgCount = func_num_args();
		// Get keys to sort by and put them to SortRule array.
		for($I = 1;$I < $ArgCount;$I ++)
		{
			$Arg = func_get_arg($I);
			if(!preg_match("/SORT/",$Arg))
			{
				$KeyNameList[] = $Arg;
				$SortRule[]    = '$'.$Arg;
			}
			else
			{
				$SortRule[]    = $Arg;
			}
		}
		// Get the values according to the keys and put them to array.
		foreach($ArrayData AS $Key => $Info)
		{
			foreach($KeyNameList AS $KeyName)
			{
				${$KeyName}[$Key] = $Info[$KeyName];
			}
		}
		// Create the eval string and eval it.
		$EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
		eval ($EvalString);
		return $ArrayData;
	}

	/**
	 * 去除不需要的值
	 * @param array $array
	 * @param array|string $filter
	 */
	static function filterValue($array,$filter){
		$filter = (array)$filter ;
		$result = (array)$array ;
		foreach ($array as $k => $v){
			if (in_array($v, $filter)){
				unset($result[$k]) ;
			}
		}
		return $result ;
	}
	
}