本文实例讲述了php数组比较实现查找连续数的方法。分享给大家供大家参考。具体如下:
$data = array(); $data[]= array("01" ,"02", "18" , "29" , "31" , "32"); $data[]= array("02" ,"09", "11" , "22" , "24" , "27"); $data[]= array("07" ,"16", "26" , "27" , "29" , "31"); $data[]=array("04", "05", "07", "10", "13", "25"); $data[]=array("02", "04", "05", "08", "19", "22"); $data[]=array("03", "04", "15", "25", "26", "30"); $data[]=array("01", "03", "06", "12", "16", "32"); $data[]=array("01", "05", "14", "17", "22"); // 判断表格中3个以上的连续 function checkAll($sourceArr2D) { $count = sizeof($sourceArr2D); for($i=0; $i<$count; $i++){ check_h($sourceArr2D[$i], $i);//找寻水平方向 if($i>0) { check_v($sourceArr2D, $i);// 找寻竖直方向 check_l($sourceArr2D, $i);// 找寻/方向 check_r($sourceArr2D, $i);// 找寻方向 } } } // 判断水平方向 $h = array();// 保存水平方向上的搜寻结果 function check_h($arr, $rownum) { //sort($arr, SORT_NUMERIC); global $h; $flag = false; for($i=2; $i<6; $i++){ if( $arr[$i-2]==$arr[$i]-2 ){ // 因为数组内数字不重复且有序故可取巧 if(!$flag) { $h[$rownum] = array($arr[$i-2], $arr[$i-1], $arr[$i]); $flag = true; }else{ array_push($h[$rownum], $arr[$i]); } } } } // 判断竖直方向 $v = array();// 保存竖直方向上的搜寻结果 function check_v($sourceArr2D, $rownum) { global $v; for($i=0; $i<6; $i++){ $val = $sourceArr2D[$rownum][$i]; if( in_array($rownum...$val, $v) ){ continue; } if( in_array($val,$sourceArr2D[$rownum-1]) && in_array($val,$sourceArr2D[$rownum+1]) ){ array_push($v, ($rownum-1)...$val, $rownum...$val, ($rownum+1)...$val); $tmp = $rownum + 2; while( ($tmp < sizeof($sourceArr2D)) && in_array($val, $sourceArr2D[$tmp]) ){ array_push($v, $tmp...$val); $tmp++; } } } } // 判断/方向 $l = array();// 保存/方向上的搜寻结果 function check_l() { } // 判断方向 $r = array();// 保存方向上的搜寻结果 function check_r() { } // 结束定义,开始应用 checkAll($data); echo //水平方向上的找寻: print_r($h); echo //竖直方向上的找寻: print_r($v); echo //方向上的找寻: print_r($l); echo //方向上的找寻: print_r($r);
希望本文所述对大家的php程序设计有所帮助。

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
