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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值