Home  >  Article  >  Backend Development  >  Detailed explanation of common PHP algorithms or functions

Detailed explanation of common PHP algorithms or functions

小云云
小云云Original
2018-03-30 13:14:551964browse

This article mainly shares with you detailed explanations of common PHP algorithms or functions. It is mainly shared with you in the form of code. I hope it can help you.

<?php

//不用第三个变量,交换两个变量的值
function changeVar()
{
    $a = &#39;123&#39;;
    $b = &#39;456&#39;;
    
    list($a, $b) = array($b, $a);
    
    echo $a . &#39;-&#39; . $b;
}
//文件锁
function fileLock()
{
    $fp = fopen(&#39;./tmp.txt&#39;, &#39;w+&#39;);
    if (flock($fp, LOCK_EX)) { //独占锁定
        fwrite($fp, &#39;write something\n&#39;);
        flock($fp, LOCK_UN);//释放锁
    } else {
        echo "can&#39;t lock!";
    }
    fclose($fp);
}

//获取文件后缀
function getExtName($url)
{
    $path_info = pathinfo($url);
    $ext_name = $path_info[&#39;extension&#39;];
    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(&#39;,&#39;, $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 = &#39;ASC&#39;)
{
    if (!is_array($arr)) {
        return false;
    }
    $keysvalue = array();
    foreach ($arr as $k => $v) {
        $keysvalue[$k] = $v[$keys];
    }

    if ($order == &#39;ASC&#39;) {
        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 != &#39;.&#39; && $filename != &#39;..&#39;) {
                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, &#39;_&#39;);
    return str_replace(&#39;_&#39;, &#39;&#39;, $str);
}

//反转每个单词
function convert($input) {
    $arr = explode(" ", $input);
    array_walk($arr, function (&$value) {
        $value = strrev($value);
    });
    $output =  implode(" ", $arr);
    echo $output;
}

Related recommendations:

2018 Common algorithm questions for front-end interviews

Detailed explanation of common algorithm questions in JavaScript interviews

Several common algorithms for PHP redemption

The above is the detailed content of Detailed explanation of common PHP algorithms or functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn