php常用功能函数收集

1、多维数组根据某项排序
private function multi_array_sort($multi_array, $sort_key, $sort=SORT_ASC){ 
		if(is_array($multi_array)){ 
			foreach ($multi_array as $row_array){ 
				if(is_array($row_array)){ 
					$key_array[] = $row_array[$sort_key]; 
				} else { 
					return false; 
				} 
			} 
		} else { 
			return false; 
		} 
		array_multisort($key_array,$sort,$multi_array); 
		return $multi_array; 
	} 

来源:http://moper.me/php-multidimensional-array-sort.html

2、多维数组合并
//合并itemsearch的结果
	private function mergeList(&$arr)
	{
		$data = array();
		$n = &$arr;
		foreach ($n as $key => $value) {
			if(!$value) continue;
			array_push($data,$this->getItem($value,$n));
		}
		return array_filter($data);
	}
	private function getItem(&$item,&$arr){
		foreach ($arr as $key => $value) {
			if(!$value) {
				continue;
			};
			if($item["itemTypeName"] == $value["itemTypeName"]
			&& $item["weight"] == $value["weight"]
			&& $item["type"] == $value["type"]){
				if($item["itemTypeId"] == $value["itemTypeId"]
					&& $item["warehouseId"] == $value["warehouseId"]){
					continue;	
				}else{
					$item["stockIn"] += $value["stockIn"];
					$item["remain"] += $value["remain"];
					$item["stockOut"] += $value["stockOut"];
					$item["stockIn"] = sprintf("%.4f",$item["stockIn"]);
					$item["remain"] = sprintf("%.4f",$item["remain"]);
					$item["stockOut"] = sprintf("%.4f",$item["stockOut"]);
					$arr[$key] = "";
				}
			}
		}
		return $item;
	}



3、对象转换成数组
function objectToArray($d) {
	if (is_object($d)) {
		// Gets the properties of the given object
		// with get_object_vars function
		$d = get_object_vars($d);
	}

	if (is_array($d)) {
		/*
		* Return array converted to object
		* Using __FUNCTION__ (Magic constant)
		* for recursive call
		*/
		return array_map(__FUNCTION__, $d);
	}
	else {
		// Return array
		return $d;
	}
}



4、 数组转换成对象
function arrayToObject($d) {
	if (is_array($d)) {
		/*
		* Return array converted to object
		* Using __FUNCTION__ (Magic constant)
		* for recursive call
		*/
		return (object) array_map(__FUNCTION__, $d);
	}
	else {
		// Return object
		return $d;
	}
}



5、HTML特殊字符转换
function htmlspecialcharsx($str) {
	$s = htmlspecialchars($str);
	return str_replace('&#', '&#', $s);
}


6、 毫秒格式数组
function microtime_format() {
    $time = number_format(microtime(true),8,'.','');
	return explode(".", $time);
}


你可能感兴趣的:(PHP)