$value) { $str .= $value . $separator; } //去掉结尾的分隔符 $str = trim($str, $separator); return $str; } /** * 把返回的数据集转换成Tree * @param array $list 要转换的数据集 * @param string $pk pk标记字段 * @param string $pid parent标记字段 * @param string $child level标记字段 * @param int $root 从第几层开始 * @date 2021-01-22 */ function listToTree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0) { // 创建Tree $tree = array(); if (is_array($list)) { // 创建基于主键的数组引用 $refer = array(); foreach ($list as $key => $data) { $refer[$data[$pk]] =& $list[$key]; } foreach ($list as $key => $data) { // 判断是否存在parent $parentId = $data[$pid]; if ($root == $parentId) { $tree[] =& $list[$key]; } else { if (isset($refer[$parentId])) { $parent =& $refer[$parentId]; $parent[$child][] =& $list[$key]; } } } } return $tree; } /** * 转换周几 * @param $int $week 周几数字格式转为大写 * @date 2021-01-22 */ function week_to_upper($week) { $week_array=array("日","一","二","三","四","五","六"); return $week_array[$week]; } /** * 时间细分 * @param int $create_time 时间戳 * @param int $type 显示格式 1--显示X天前 2--显示年月日时分 * @date 2021-01-22 */ function timeRefine($create_time, $type = 1) { $time = time() - $create_time; $days = intval($time / 86400); //计算小时数 $remain = $time % 86400; $hours = intval($remain / 3600); //计算分钟数 $remain = $remain % 3600; $mins = intval($remain / 60); //计算秒数 $secs = $remain % 60; if ($days != 0) { if ($type == 1) { $time = $days . '天前'; } else { $time = date('Y-m-d H:i', $create_time); } } elseif ($hours != 0) { $time = $hours . '小时前'; } elseif ($mins != 0) { $time = $mins . '分钟前'; } elseif ($secs != 0) { $time = $secs . '秒前'; } else { $time = '刚刚'; } return $time; } /** * 数组或对象转XML * @param string|object $data 数据 * @param string $parentNode xml * @date 2021-01-22 */ function dataToXml($data, $parentNode = null) { //如果父节点为null,则创建root节点,否则就使用父节点 if ($parentNode === null) { $simple_xml = new SimpleXMLElement(''); } else { $simple_xml = $parentNode; } //遍历数组 foreach ($data as $key => $value) { /* 指定默认的数字key */ is_numeric($key) && $key = 'item'; /* 添加子元素 */ if (is_array($value) || is_object($value)) { dataToXml($value, $simple_xml->addChild($key)); } else { if (is_numeric($value)) { $child = $simple_xml->addChild($key, $value); } else { $child = $simple_xml->addChild($key); $node = dom_import_simplexml($child); $node->appendChild($node->ownerDocument->createCDATASection($value)); } } } return $simple_xml->asXML(); } /** * xml转数组 */ function xml_to_array($data) { $obj = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA); $json = json_encode($obj); $arr = json_decode($json, true); return $arr; } /** * 对象转化为数组 * @param object $object 对象 * @date 2021-01-22 */ function objectToArray($obj) { $obj = (array)$obj; foreach ($obj as $k => $v) { if (gettype($v) == 'resource') { return; } if (gettype($v) == 'object' || gettype($v) == 'array') { $obj[$k] = (array)objectToArray($v); } } return $obj; } /** * 数组转化为对象 * @param array $arr 数组 * @date 2021-01-22 */ function arrayToObject($arr) { if (gettype($arr) != 'array') { return; } foreach ($arr as $k => $v) { if (gettype($v) == 'array' || getType($v) == 'object') { $arr[$k] = (object)arrayToObject($v); } } return (object)$arr; } /** * 文件相对路径转绝对路径 * @param string $url 文件链接 * @date 2021-01-22 */ function urlToAbsolute($link_url = '') { if (stripos($link_url, 'http') === false) { return \think\facade\Request::domain() . $link_url; } else { return $link_url; } } /** * 格式化参数格式化成url参数 * @param array $arr 需要转换的参数 * @date 2022-06-08 */ function arrayToUrlParams($arr) { $str = ''; //以&连接,拼接为key=value&key=value foreach ($arr as $k => $v) { // 参数值为空,则不参与签名 if (!empty($v)) { $str .= $k . '=' . $v . '&'; } } //去除末尾的& $str = trim($str, '&'); return $str; } /** * URL安全的字符串编码 * @param string $data * @date 2022-06-30 * @return string */ function base64url_encode($data) { return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); } /** * URL安全的字符串解码 * @param string $data * @date 2022-06-30 * @return string */ function base64url_decode($data) { return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT)); }