php自定義二維數(shù)組排序函數(shù)array_orderby用法示例
本文實(shí)例講述了php自定義二維數(shù)組排序函數(shù)array_orderby用法。分享給大家供大家參考,具體如下:
<?php /** I came up with an easy way to sort database-style results. This does what example 3 does, except it takes care of creating those intermediate arrays for you before passing control on to array_multisort(). */ function array_orderby() { $args = func_get_args(); $data = array_shift($args); foreach ($args as $n => $field) { if (is_string($field)) { $tmp = array(); foreach ($data as $key => $row) $tmp[$key] = $row[$field]; $args[$n] = $tmp; } } $args[] = &$data; call_user_func_array('array_multisort', $args); return array_pop($args); } /* The sorted array is now in the return value of the function instead of being passed by reference. */ $data[] = array('volume' => 67, 'edition' => 2); $data[] = array('volume' => 86, 'edition' => 1); $data[] = array('volume' => 85, 'edition' => 6); $data[] = array('volume' => 98, 'edition' => 2); $data[] = array('volume' => 86, 'edition' => 6); $data[] = array('volume' => 67, 'edition' => 7); // Pass the array, followed by the column names and sort flags $sorted = array_orderby($data, 'volume', SORT_DESC, 'edition', SORT_ASC); print_r($sorted) ?>
運(yùn)行結(jié)果:
Array ( [0] => Array ( [volume] => 98 [edition] => 2 ) [1] => Array ( [volume] => 86 [edition] => 1 ) [2] => Array ( [volume] => 86 [edition] => 6 ) [3] => Array ( [volume] => 85 [edition] => 6 ) [4] => Array ( [volume] => 67 [edition] => 2 ) [5] => Array ( [volume] => 67 [edition] => 7 ) )
PS:這里再為大家推薦一款關(guān)于排序的演示工具供大家參考:
在線動(dòng)畫演示插入/選擇/冒泡/歸并/希爾/快速排序算法過程工具:
http://tools.jb51.net/aideddesign/paixu_ys
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》及《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
淺析Yii中使用RBAC的完全指南(用戶角色權(quán)限控制)
本篇文章是對Yii中使用RBAC的完全指南(用戶角色權(quán)限控制)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP7實(shí)現(xiàn)和CryptoJS的AES加密方式互通示例【AES-128-ECB加密】
這篇文章主要介紹了PHP7實(shí)現(xiàn)和CryptoJS的AES加密方式互通操作,結(jié)合實(shí)例形式分析了PHP AES-128-ECB加密算法相關(guān)使用技巧,需要的朋友可以參考下2019-06-06PHP實(shí)現(xiàn)的oracle分頁函數(shù)實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)的oracle分頁函數(shù),結(jié)合實(shí)例形式分析了PHP針對oracle數(shù)據(jù)庫使用rownum代替MySQL中l(wèi)imit實(shí)現(xiàn)的分頁操作相關(guān)技巧,需要的朋友可以參考下2016-01-01PHP中array_merge和array相加的區(qū)別分析
今天處理一個(gè)這樣的問題:如何獲取字符鍵名相同值不同的兩個(gè)數(shù)組值集合,用array_merge和數(shù)組相加都不可行,讓我認(rèn)真比較了下PHP中array_merge和array相加的區(qū)別2013-06-06PHP基于MySQL數(shù)據(jù)庫實(shí)現(xiàn)對象持久層的方法
這篇文章主要介紹了PHP基于MySQL數(shù)據(jù)庫實(shí)現(xiàn)對象持久層的方法,實(shí)例分析了php實(shí)現(xiàn)持久層的相關(guān)技巧,需要的朋友可以參考下2015-06-06PHP面向?qū)ο蟪绦蛟O(shè)計(jì)繼承用法簡單示例
這篇文章主要介紹了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)繼承用法,結(jié)合具體實(shí)例形式分析了php面向?qū)ο蟪绦蛟O(shè)計(jì)中繼承的相關(guān)概念、原理、使用技巧與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-12-12