错误整理

前端之家收集整理的这篇文章主要介绍了错误整理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

array_column unset

1,"pid"=>0,"views"=>100
    ],[
        "uid"=>2,"pid"=>1,"views"=>200
    ],[
        "uid"=>3,"views"=>300
    ],[
        "uid"=>4,"views"=>400
    ],[
        "uid"=>5,"pid"=>3,"views"=>500
    ]
);
//https://laravel-china.org/articles/16778
array_search('3',array_column($items,'uid'));// 2 uid =3所在的数据
unset($items[1]);
>>> $items
=> [
     0 => [
       "uid" => 1,"pid" => 0,"views" => 100,],2 => [
       "uid" => 3,"views" => 300,3 => [
       "uid" => 4,"views" => 400,4 => [
       "uid" => 5,"pid" => 3,"views" => 500,]
>>> array_search(3,'uid'))
=> 1
>>> $r=array_combine(array_keys($items),'uid'));
=> [
     0 => 1,2 => 3,3 => 4,4 => 5,$r)
=> 2

斐波那契数列

return fibonacci_recursive($n - 1) + fibonacci_recursive($n - 2);

}

for ($i = 1; $i <= 30; $i++) {
echo fibonacci_recursive($i) . " ";
}

耗时
3.52s user 0.00s system 99% cpu 3.521 total
function fibonacci_recursive_optimization($n) {
static $caches_arr = [];

if (isset($caches_arr[$n])) {
    return $caches_arr[$n];
}

if ($n <= 1) {
    $res = 1;
} else {
    $res = fibonacci_recursive_optimization($n - 1) + fibonacci_recursive_optimization($n - 2);
}

$caches_arr[$n] = $res;

return $res;

}

for ($i = 1; $i <= 30; $i++) {
echo fibonacci_recursive_optimization($i) . " ";
}

耗时
0.01s user 0.01s system 98% cpu 0.022 total
function fibonacci_closure() {
static $x = 0;
static $y = 1;
return function () use (&$x,&$y) {
[$x,$y] = [$y,$x + $y];
return $y;
};
}

$f = fibonacci_closure();
for ($i = 1; $i <= 30; $i++) {
echo $f() . " ";
}

耗时
0.02s user 0.01s system 99% cpu 0.030 total

Intervention/Image

text('测试写入中文',function($font) {
    $font->file('foo/bar.ttf');
    $font->size(24);
    $font->color('#fdf6e3');
    $font->align('center');
    $font->valign('top');
    $font->angle(45);
});
function to_unicode($string)
{
    $str = mb_convert_encoding($string,'UCS-2','UTF-8');
    $arrstr = str_split($str,2);
    $unistr = '';
    foreach ($arrstr as $n) {
        $dec = hexdec(bin2hex($n));
        $unistr .= '&#' . $dec . ';';
    }
    return $unistr;
}

数据库查询中对 like 的值进行转义

where('title','like',"%".$keyword."%");https://laravel-china.org/topics/16945
function escape_like_str($str)
{
    $like_escape_char = '!';
return str_replace([$like_escape_char,'%','_'],[
    $like_escape_char.$like_escape_char,$like_escape_char.'%',$like_escape_char.'_',$str);

}
$query->where('title',"%".escape_like_str($keyword)."%");

原文链接:https://www.f2er.com/note/413547.html

猜你在找的程序笔记相关文章