PHP loop, V for key, C for value
$list Array ("AAA" = "TTT", "bbb" = "SSS" ); foreach ($listas $v$c) { echo$v; Echo $c ;};
Generates a fixed-length 62 binary random code (0-9, A-Z, A-Z)
functionGetrandom ($param){ $str= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $key= ""; for($i= 0;$i<$param;$i++) { $key.=$str{Mt_rand(0,32)};//Generate PHP random number } return $key;}
SQL recursive query
SELECTsort_id asId,sort_parent asParent ID, levels asProgression between parent to child, paths asParent to Child path from ( SELECTSort_id,sort_parent,@le:= IF(sort_parent= 0,0, IF(LOCATE (CONCAT ('|', Sort_parent,':'),@pathlevel)> 0, Substring_index (Substring_index (@pathlevel, CONCAT ('|', Sort_parent,':'),-1),'|',1)+1 ,@le+1)) levels,@pathlevel:=CONCAT (@pathlevel,'|', sort_id,':',@le,'|') Pathlevel,@pathnodes:= IF(sort_parent=0,', 0', Concat_ws (',', IF(LOCATE (CONCAT ('|', Sort_parent,':'),@pathall)> 0, Substring_index (Substring_index (@pathall, CONCAT ('|', Sort_parent,':'),-1),'|',1) ,@pathnodes) , sort_parent) paths,@pathall:=CONCAT (@pathall,'|', sort_id,':',@pathnodes,'|') Pathall fromFlow_sort, (SELECT @le:=0,@pathlevel:="',@pathall:="',@pathnodes:="') VVORDER bysort_parent,sort_id) srcORDER bysort_id
PHP Two-dimensional arrays remove duplicate values, retain key values, restore row indexes, automatically modify the column index, if only less than 2 rows (no ordering necessary), directly return the original array.
protected functionARRAY_UNIQUE_FB ($array 2D){ if(Count($array 2D) >1){ $keyarr= []; foreach($array 2D[0] as $k=$v){ $keyarr[] =$k; }foreach($array 2D as $k=$v){ $v=Join(‘,‘,$v);//dimension, you can also convert one-dimensional array to a comma-concatenated string using implode $temp[$k]=$v; } $temp=Array_unique($temp);//Remove duplicate strings, that is, duplicate one-dimensional arrays foreach($temp as $k=$v){ $array=Explode(‘,‘,$v);//re-assemble the disassembled array//(the index below will be modified according to your own situation)//auto-reply index foreach($keyarr as $sk=$SV){ $temp 2[$k][$SV] =$array[$sk]; } } $temp 2=array_values($temp 2); }Else{ $temp 2=$array 2D; }return $temp 2;}
To cache a page
functionPagecache ($siteId,$page= ",$expire= 7200){ $siteId=$siteId?: 1; $fileName=$siteId.‘. html; $filePath= Cache_path.$fileName; if($page){ file_put_contents($filePath,$page); }Else{ if(file_exists($filePath)){ $editTime=Fileatime($filePath); $page= ' '; if(( Time() -$editTime) <=$expire){ $page=file_get_contents($filePath); } } } if($page){ Echo $page; Exit; }}
Array output JSON, note that if there are cross-domain operations (such as an API site), you need to add a cross-domain HTTP header
$arr = [ ' haha ' = + , ' tata ' = ' Shiningsun ', ' lala ' = [15,16] ,]; Header ("access-control-allow-origin:*"); $result = Json_encode ($arr);
PHP Traversal file name
/*********************** with Readdir () function ************************/functionListdir ($dir){ if(Is_dir($dir)) { if($DH=Opendir($dir)) { $arr=Array(); while(($file=Readdir($DH)) !==false) { if((Is_dir($dir." /".$file)) &&$file! = "." &&$file!="..") { $arr[$file] = Listdir ($dir." /".$file." /"); } Else { if($file! = "." &&$file!="..") { $arr[$file] = Get_extension ($file); } } } Closedir($DH); return $arr; } }}/*********************** Get file name extension ************************/functionGet_extension ($file){ return PathInfo($file,pathinfo_extension);}
Reading page information from the URL
$url= ' http://school.51cce.dev/klzhhdx/news/'; $use _include_path false ; $context NULL ; $offset =-1; $contents file_get_contents ($url$use _include_path$context$offset);d UMP ($ Contents);
Thinkphp 5.0 Summary: Receive parameters
$data [' QQ '] = Request::instance ()->post (' QQ '); Request$data[' email '] = Request::instance ()->post (' email '); Request$data[' Contactstel '] = Request::instance ()->post (' Mobile ');
Determine if there is a post value (get same)
if (Request::instance ()->ispost ())
Three: PHP, THINKPHHP, and SQL