时间:2021-07-01 10:21:17 帮助过:6人阅读
<?php //不用第三个变量,交换两个变量的值 function changeVar() { $a = '123'; $b = '456'; list($a, $b) = array($b, $a); echo $a . '-' . $b; } //文件锁 function fileLock() { $fp = fopen('./tmp.txt', 'w+'); if (flock($fp, LOCK_EX)) { //独占锁定 fwrite($fp, 'write something\n'); flock($fp, LOCK_UN);//释放锁 } else { echo "can't lock!"; } fclose($fp); } //获取文件后缀 function getExtName($url) { $path_info = pathinfo($url); $ext_name = $path_info['extension']; echo $ext_name; } /*斐波那契数列第n位是什么,递归实现 * @param int $n 位数 */ function fibonacci($n) { $return = 1; if ($n <= 0) { $return = 0; } elseif ($n <= 2) { $return = 1; } else { $return = fibonacci($n - 2) + fibonacci($n - 1); } return $return; } //自己实现number_format function numFormat($num) { // return number_format($num); $num = strrev($num);//反转 $num = str_split($num, 3);//分割成数组 $num = implode(',', $num);//拼接 $num = strrev($num);//再反转 return $num; } //冒泡排序 function bubble_sort(&$arr) { $len = count($arr); for ($i = 0; $i < $len; $i++) { for ($j = 1; $j < $len - $i; $j++) { if ($arr[$j - 1] > $arr[$j]) { $tmp = $arr[$j]; $arr[$j] = $arr[$j - 1]; $arr[$j - 1] = $tmp; } } } } //快速排序 function quickSort($arr) { $len = count($arr); if ($len <= 1) { return $arr; } $base = $arr[0]; $left_array = array(); $right_array = array(); for ($i = 1; $i < $len; $i++) { if ($arr[$i] > $base) { $left_array[] = $arr[$i]; } else { $right_array[] = $arr[$i]; } } $left_array = quickSort($left_array); $right_array = quickSort($right_array); return $left_array; } //二维数组排序 function array_sort($arr, $keys, $order = 'ASC') { if (!is_array($arr)) { return false; } $keysvalue = array(); foreach ($arr as $k => $v) { $keysvalue[$k] = $v[$keys]; } if ($order == 'ASC') { asort($keysvalue); } else { arsort($keysvalue); } reset($keysvalue); foreach ($keysvalue as $k => $v) { $keysort[$k] = $k; } foreach ($keysort as $k => $v) { $new_arr[] = $arr[$v]; } return $new_arr; } //使用自带函数二维数组排序 function array_sort2(&$arr, $keys, $order = SORT_ASC) { array_multisort(array_column($arr, $keys), $order, $arr); } //二分查找 function bin_sch($arr, $low, $top, $target) { sort($arr); while ($low <= $top) { $mid = floor(($low + $top)/2); if ($arr[$mid] == $target) { return $arr[$mid]; } elseif ($arr[$mid] < $target) { $low = $mid + 1; bin_sch($arr, $low, $top, $target); } else { $top = $mid - 1; bin_sch($arr, $low, $top, $target); } } return -1; } //遍历文件夹 function my_scandir($dir) { $files = array(); if ($handle = opendir($dir)) { while (($filename = readdir($handle)) !== false) { if ($filename != '.' && $filename != '..') { if (is_dir($dir."/".$filename)) { $files[$filename] = my_scandir($dir."/".$filename); } else { $files[] = $filename; } } } closedir($handle); return $files; } } //get_user_id改为getUserId function change($str) { $str = ucwords($str, '_'); return str_replace('_', '', $str); } //反转每个单词 function convert($input) { $arr = explode(" ", $input); array_walk($arr, function (&$value) { $value = strrev($value); }); $output = implode(" ", $arr); echo $output; }
相关推荐:
2018前端面试常见算法题
JavaScript 面试中常见算法问题详解
PHP兑现的几种常见算法
以上就是PHP常见算法或函数详解的详细内容,更多请关注Gxl网其它相关文章!