<?PHP

/**
* array functions
*/
class ArrayClass{
	/**
	* array multisort
	* @param Array $array - array(array('key' => value))
	* @param String $by - 'key'
	* @param String $order - ASC,DESC
	* @param String $type - string,num
	*/
	function MultiSort($array, $by, $order, $type){

		$sortby = "sort$by"; //This sets up what you are sorting by

		$firstval = current($array); //Pulls over the first array

		$vals = array_keys($firstval); //Grabs the associate Arrays

		foreach ($vals as $init){
		   $keyname = "sort$init";
		   $$keyname = array();
		}

		foreach ($array as $key => $row) {
  
			foreach ($vals as $names){
			   $keyname = "sort$names";
			   $test = array();
			   $test[$key] = $row[$names];
			   $$keyname = array_merge($$keyname,$test);
			}
		}

		if ($order == "DESC"){   
			if ($type == "num"){
				array_multisort($$sortby,SORT_DESC, SORT_NUMERIC,$array);
			} 
			else {
				array_multisort($$sortby,SORT_DESC, SORT_STRING,$array);
			}
		} 
		else {
			if ($type == "num"){
				array_multisort($$sortby,SORT_ASC, SORT_NUMERIC,$array);
			} 
			else {
				array_multisort($$sortby,SORT_ASC, SORT_STRING,$array);
			}
		}

		return $array;
	}
}

?>
