0 && !isset($item["folder"])) { $item["folder"] = true; } $return[] = $item; } } return $return; } /** * tree 转一维数组 * @param $array * @return array */ function flatten_tree($array, $key = "children") { $result = []; foreach ($array as $item) { $result[] = $item; if (count($item[$key]) > 0) { $result = array_merge($result, flatten_tree($item[$key], $key)); } } return $result; } /** * 根据pid找寻所有的上层节点 * @param $array * @param $val * @param string $parent_key * @param array $result * @return array */ function get_pid($array, $val, $parent_key = "pid", $result = []) { $array = array_by_key($array, "id"); if (!isset($array[$val])) { return $result; } if ($array[$val][$parent_key] == 0 || !isset($array[$array[$val][$parent_key]])) { return $result; } else { $result[] = $array[$val][$parent_key]; return get_pid($array, $array[$val][$parent_key], $parent_key, $result); } } /** * 根据平行数组递归查找子节点 * @param $array * @param $key * @param $parent_key * @param $val * @param array $return * @return array */ function find_children($array, $key, $parent_key, $val, $return = []) { foreach ($array as $item) { if ($item[$parent_key] == $val) { $return[] = $item; $return = find_children($array, $key, $parent_key, $item[$key], $return); } } return $return; } /** * 18位身份证转年龄 * @param $IDnumber * @return bool|string */ function id_number2age($IDnumber) { $year = (int)substr($IDnumber, 6, 4); return date("Y") - $year; } /** * * @param $num * @param $lower * @return float|int|mixed|string */ function number2chinese($num, $lower = false) { $c1 = "零壹贰叁肆伍陆柒捌玖"; $c2 = "分角元拾佰仟万拾佰仟亿"; if ($lower) { $c1 = "零一二三四五六七八九"; $c2 = "分角元十佰仟万拾佰仟亿"; } $num = round($num, 2); $num = $num * 100; if (strlen($num) > 11) { return $num; } $i = 0; $c = ""; while (1) { if ($i == 0) { $n = substr($num, strlen($num) - 1, 1); } else { $n = $num % 10; } $p1 = substr($c1, 3 * $n, 3); $p2 = substr($c2, 3 * $i, 3); if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) { $c = $p1 . $p2 . $c; } else { $c = $p1 . $c; } $i = $i + 1; $num = $num / 10; $num = (int)$num; if ($num == 0) { break; } } $j = 0; $slen = strlen($c); while ($j < $slen) { $m = substr($c, $j, 6); if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') { $left = substr($c, 0, $j); $right = substr($c, $j + 3); $c = $left . $right; $j = $j - 3; $slen = $slen - 3; } $j = $j + 3; } if (substr($c, strlen($c) - 3, 3) == '零') { $c = substr($c, 0, strlen($c) - 3); } if (empty($c)) { return "零"; } else { return str_replace("元", "", $c); } } /** * 替换字符串中的部分字符串为星号 * @param null $string * @return string|null */ function string2secret($string = NULL) { if (!$string) { return NULL; } $length = mb_strlen($string); $visibleCount = (int)round($length / 4); $hiddenCount = $length - ($visibleCount * 2); return mb_substr($string, 0, $visibleCount) . str_repeat('*', $hiddenCount) . mb_substr($string, ($visibleCount * -1), $visibleCount); } /** * 友好的时间显示 * * @param int $sTime 待显示的时间 * @param string $type 类型. normal | mohu | full | ymd | other * @param string $alt 已失效 * @return string */ function friendly_date($sTime, $type = 'normal', $alt = 'false') { if (!$sTime) return ''; //sTime=源时间,cTime=当前时间,dTime=时间差 $sTime = strtotime($sTime); $cTime = time(); $dTime = $cTime - $sTime; $after_or_before = "前"; $dDay = intval(date("z", $cTime)) - intval(date("z", $sTime)); $dYear = intval(date("Y", $cTime)) - intval(date("Y", $sTime)); if ($sTime > $cTime) { $dTime = $sTime - $cTime; $after_or_before = "后"; $dDay = intval(date("z", $sTime)) - intval(date("z", $cTime)); $dYear = intval(date("Y", $sTime)) - intval(date("Y", $cTime)); } //normal:n秒前,n分钟前,n小时前,日期 if ($type == 'normal') { if ($dTime < 60) { if ($dTime < 10) { return '刚刚'; //by yangjs } else { return intval(floor($dTime / 10) * 10) . "秒{$after_or_before}"; } } elseif ($dTime < 3600) { return intval($dTime / 60) . "分钟{$after_or_before}"; //今天的数据.年份相同.日期相同. } elseif ($dYear == 0 && $dDay == 0) { //return intval($dTime/3600)."小时前"; return '今天' . date('H:i', $sTime); } elseif ($dYear == 0) { return date("m月d日 H:i", $sTime); } else { return date("Y-m-d H:i", $sTime); } } elseif ($type == 'mohu') { if ($dTime < 60) { return $dTime . "秒{$after_or_before}"; } elseif ($dTime < 3600) { return intval($dTime / 60) . "分钟{$after_or_before}"; } elseif ($dTime >= 3600 && $dDay == 0) { return intval($dTime / 3600) . "小时{$after_or_before}"; } elseif ($dDay > 0 && $dDay <= 7) { return intval($dDay) . "天{$after_or_before}"; } elseif ($dDay > 7 && $dDay <= 30) { return intval($dDay / 7) . "周{$after_or_before}"; } elseif ($dDay > 30) { return intval($dDay / 30) . "个月{$after_or_before}"; } //full: Y-m-d , H:i:s } elseif ($type == 'full') { return date("Y-m-d , H:i:s", $sTime); } elseif ($type == 'ymd') { return date("Y-m-d", $sTime); } else { if ($dTime < 60) { return $dTime . "秒{$after_or_before}"; } elseif ($dTime < 3600) { return intval($dTime / 60) . "分钟{$after_or_before}"; } elseif ($dTime >= 3600 && $dDay == 0) { return intval($dTime / 3600) . "小时{$after_or_before}"; } elseif ($dYear == 0) { return date("Y-m-d H:i:s", $sTime); } else { return date("Y-m-d H:i:s", $sTime); } } } /** * 多行文本转为radio选项 * @param $text * @param string $value * @param bool $block * @return string */ function multiline2radio($text, $value = "", $block = false) { $res = '