8 PHP数组字符串,组装拆分

<?php
/*
 * 数组字符串	组装拆分
 * 传进来数组,返回字符串或者false
 * 		array(0,1)						返回0#1	
 * 		array(array(0,1),array(3,4))	返回0#1*3#4
 * 传进来字符串,返回数组或者false			
 * 		0#1								返回array(0,1)
 * 		0#1*3#4							返回array(array(0,1),array(3,4))	
 * 
 * 
 * $arr_str					数组或者字符串
 * $sep1					分隔符1
 * $sep2					分隔符2
 * $outputArrNum			返回几维数组(当字符串时一维数组时)
 * 不能转换键名
 */
class arrayStringTrans{
	static public function trans($arr_str,$sep1="#",$sep2="*",$outputArrNum=1){
		if(is_array($arr_str) && !empty($arr_str)){
			$num	=	self::getmaxdim($arr_str); 
			switch ($num){
				case 1:
				$result	=	self::combine_array1($arr_str,$sep1);
				return $result;
				break;
 
				case 2:
				$result	=	self::combine_array2($arr_str,$sep1,$sep2);
				return $result;
				break;		
 
				default:
				return false;
			}
		}else if(is_string($arr_str) && !empty($arr_str)){
			if(strstr($arr_str,$sep1) &&  strstr($arr_str,$sep2)){
				$result	=	self::chaifen_array2($arr_str,$sep1,$sep2);
				return $result;
			}else if(strstr($arr_str,$sep1)){
				if($outputArrNum==1){
					$result	=	self::chaifen_array1($arr_str,$sep1);
					return $result;
				}else{
					$result	=	self::chaifen_array2($arr_str,$sep1,$sep2);
					return $result;
				}
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
 
	/*
	 * 组装二维数组成字符串
	 */
	static public function combine_array2($array,$sep1,$sep2){				
		$str	="";
		foreach($array as $val){
			foreach($val as $v){
				$str	.=	$v.$sep1;
			}
		$str	=	rtrim($str,$sep1);
		$str	.=  $sep2;
		}
		$str	=	rtrim($str,$sep2);	
		return $str;
	}
 
 
	/*
	 * 拆分字符串成二维数组
	 */
	static public function chaifen_array2($str,$sep1,$sep2){					
		$new_array		=	explode($sep2,$str);
		foreach ($new_array as $val){
			$new_arrays[]=	explode($sep1,$val);	
		}
		return $new_arrays;
	}
 
 
	/*
	 * 组装一维数组成字符串
	 */
	static public function combine_array1($array,$sep1){				
		$str	="";
			foreach($array as $val){
				$str	.=	$val.$sep1;
			}	
			$str	=	rtrim($str,$sep1);
		return $str;
	}
 
 
	/*
	 * 拆分字符串成一维数组
	 */
	static public function chaifen_array1($str,$sep1){					
		$new_array		=	explode($sep1,$str);
		return $new_array;
	}
 
 
	/*
	 * 判断是几维数组
	 */
	static public function getmaxdim($vDim){
		if (! is_array ( $vDim )) {
			return 0;
		} else {
			$max1 = 0;
				foreach ( $vDim as $item1 ) {
					$t1 = self::getmaxdim ( $item1 );
					if ($t1 > $max1){
						$max1 = $t1;
					}	
				}
				return $max1 + 1;
		}
	}
}
//$arr	=	array(array('00',11),array(33,44));
//$arr	=	'0#1';
//var_dump(arrayStringTrans::trans($arr,'#','*',2));
?>