對多維數組排序,通用的作法是
1 獲取利用排序的資料並且將其放入數組$arrSort. 其中鍵索引為要排序數組的索引,保證唯一性
2 利用排序函數sort等對$arrSort進行排序.
3 遍歷$arrSort, 根據其索引,獲取多維數組的數據,重新構造排序後的多維數組.
Array ( [0] => Array ( [link] => test [name] => test.rpm [type] => file [size] => 988.9k [mtime] => 1185160178) .... )
I 很久以前在網上找到的一個排序函數,談不上高效,但很實用 II
_array_sort($arrFile, 1, 1);//根据name字段排序 _array_sort($arrFile, 3, 1);//根据size字段排序 /* @records 要排序的数组 @field要排序的字段,注意是数字 @reverse正序还是反序 */ function _array_sort($records, $field, $reverse, $defaultSortField = 0) { $uniqueSortId = 0; $hash = array(); $sortedRecords = array(); $tempArr = array(); $indexedArray = array(); $recordArray = array(); foreach($records as $record) { $uniqueSortId++; $recordStr = implode("|", $record)."|".$uniqueSortId; $recordArray[] = explode("|", $recordStr); } $primarySortIndex = count($record); $records = $recordArray; foreach($records as $record) { $hash[$record[$primarySortIndex]] = $record[$field]; } uasort($hash, "strnatcasecmp"); if($reverse) $hash = array_reverse($hash, true); $valueCount = array_count_values($hash); foreach($hash as $primaryKey => $value) { $indexedArray[] = $primaryKey; } $i = 0; foreach($hash as $primaryKey => $value) { $i++; if($valueCount[$value] > 1) { foreach($records as $record) { if($primaryKey == $record[$primarySortIndex]) { $tempArr[$record[$defaultSortField]."__".$i] = $record; break; } } $index = array_search($primaryKey, $indexedArray); if(($i == count($records)) || ($value != $hash[$indexedArray[$index+1]])) { uksort($tempArr, "strnatcasecmp"); if($reverse) $tempArr = array_reverse($tempArr); foreach($tempArr as $newRecs) { $sortedRecords [] = $newRecs; } $tempArr = array(); } } else { foreach($records as $record) { if($primaryKey == $record[$primarySortIndex]) { $sortedRecords[] = $record; break; } } } } return $sortedRecords; }II用array_map和array_mutisort來排序
array_mutisort還可以根據多個值來進行二次或者三次排序,這是上一個函數所不能比的.
利用array_map获取要依据排序的数组 $arrField = array_map(create_function('$n', 'return $n["size"];'), $arrFile); //利用array_mutisort来进行排序 $array_multisort($arrField, SORT_DESC, $arrFile);
III 最終測試
以188個資料的排序數組進行測試條資料的排序數組進行測試條的排序數組50次求平均.
第一種方式
0.04269016 name
0.04267142 size
第二種方式
0.001249 name
第二種方式
0.001249 name
0.00083 map array_multisort 高效處理多維數組排序相關文章請關注PHP中文網!

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

使用依賴注入(DI)的原因是它促進了代碼的松耦合、可測試性和可維護性。 1)使用構造函數注入依賴,2)避免使用服務定位器,3)利用依賴注入容器管理依賴,4)通過注入依賴提高測試性,5)避免過度注入依賴,6)考慮DI對性能的影響。

phpperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovessetimes.2)優化

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

TOOPTIMIZEPHPAPPLICITIONSFORPERSTORANCE,USECACHING,數據庫imization,opcodecaching和SererverConfiguration.1)InlumentCachingWithApcutCutoredSatfetchTimes.2)優化的atabasesbasesebasesebasesbasesbasesbaysbysbyIndexing,BeallancingAndWriteExing

依賴性注射inphpisadesignpatternthatenhancesFlexibility,可檢驗性和ManiaginabilybyByByByByByExternalDependencEctenceScoupling.itallowsforloosecoupling,EasiererTestingThroughMocking,andModularDesign,andModularDesign,butquirscarecarefulscarefullsstructoringDovairing voavoidOverOver-Inje

PHP性能優化可以通過以下步驟實現:1)在腳本頂部使用require_once或include_once減少文件加載次數;2)使用預處理語句和批處理減少數據庫查詢次數;3)配置OPcache進行opcode緩存;4)啟用並配置PHP-FPM優化進程管理;5)使用CDN分發靜態資源;6)使用Xdebug或Blackfire進行代碼性能分析;7)選擇高效的數據結構如數組;8)編寫模塊化代碼以優化執行。

opcodecachingsimplovesphperforvesphpermance bycachingCompiledCode,reducingServerLoadAndResponSetimes.1)itstorescompiledphpcodeinmemory,bypassingparsingparsingparsingandcompiling.2)useopcachebachebachebachebachebachebachebysettingparametersinphametersinphp.ini,likeememeryconmorysmorysmeryplement.33)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver CS6
視覺化網頁開發工具

WebStorm Mac版
好用的JavaScript開發工具