PHP常見演算法或函數詳解

來源:互聯網
上載者:User
本文主要和大家分享PHP常見演算法或函數詳解,主要以代碼的方式和大家分享,希望能協助到大家。

<?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_formatfunction 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改為getUserIdfunction 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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.