In php I have a numerical array of associative arrays:
在php中我有一个关联数组的数值数组:
mainArray: [ array1:['title':'Record a','order':'2'], array2:['title':'Record b','order':'4'], array3:['title':'Record c','order':'1'], array4:['title':'Record d','order':'3']]
What is the simplest way to sort mainArray by the 'order' value of each associative array?
通过每个关联数组的“顺序”值对mainArray进行排序的最简单方法是什么?
Thanks
2 个解决方案
#1
You can use usort
function. Since PHP 5.4 you can use closure function:
您可以使用usort功能。从PHP 5.4开始,您可以使用闭包功能:
usort($mainArray, function ($a, $b) { $a_val = (int) $a['order']; $b_val = (int) $b['order']; if($a_val > $b_val) return 1; if($a_val < $b_val) return -1; return 0;});
Or version for PHP < 5.4:
或者PHP版本<5.4:
usort($mainArray, 'myCompare');function myCompare($a, $b) { $a_val = (int) $a['order']; $b_val = (int) $b['order']; if($a_val > $b_val) return 1; if($a_val < $b_val) return -1; return 0;}
#2
I found this in the php documentation comments for asort() See also the sort() page, in the comments there are a few good candidates.
我在php文档评论中发现了这个asort()另见sort()页面,在评论中有几个好的候选人。
function named_records_sort($named_recs, $order_by, $rev=false, $flags=0){// Create 1-dimensional named array with just // sortfield (in stead of record) values $named_hash = array(); foreach($named_recs as $key=>$fields) $named_hash["$key"] = $fields[$order_by]; // Order 1-dimensional array, // maintaining key-value relations if($reverse) arsort($named_hash,$flags=0) ; else asort($named_hash, $flags=0); // Create copy of named records array // in order of sortarray $sorted_records = array(); foreach($named_hash as $key=>$val) $sorted_records["$key"]= $named_recs[$key];return $sorted_records;} // named_recs_sort()function show_sorted_records($named_recs, $order_by, $rev=false, $flags=0){$sorted_records=named_records_sort($named_recs, $order_by, $rev, $flags);foreach($sorted_records as $name=>$fields) {echo "<b>$name</b> "; foreach($fields as $field=>$val) echo "$field = $val "; echo "<br>";}} // show_sorted_records()$girl_friends=array();$girl_friends["Anna"]=array("born"=>'1989-08-22',"cupsize"=>'B-',"IQ"=>105, "daddy"=>'rich');$girl_friends["Zoe"]=array("born"=>'1978-03-11',"cupsize"=>'C#',"IQ"=>130, "daddy"=>'poor');$girl_friends["Lilly"]=array("born"=>'1985-06-16',"cupsize"=>'DD',"IQ"=>90, "daddy"=>'nasty');$order_by="cupsize"; echo "And the winners are: <br>";show_sorted_records($girl_friends, $order_by, true);
#1
You can use usort
function. Since PHP 5.4 you can use closure function:
您可以使用usort功能。从PHP 5.4开始,您可以使用闭包功能:
usort($mainArray, function ($a, $b) { $a_val = (int) $a['order']; $b_val = (int) $b['order']; if($a_val > $b_val) return 1; if($a_val < $b_val) return -1; return 0;});
Or version for PHP < 5.4:
或者PHP版本<5.4:
usort($mainArray, 'myCompare');function myCompare($a, $b) { $a_val = (int) $a['order']; $b_val = (int) $b['order']; if($a_val > $b_val) return 1; if($a_val < $b_val) return -1; return 0;}
#2
I found this in the php documentation comments for asort() See also the sort() page, in the comments there are a few good candidates.
我在php文档评论中发现了这个asort()另见sort()页面,在评论中有几个好的候选人。
function named_records_sort($named_recs, $order_by, $rev=false, $flags=0){// Create 1-dimensional named array with just // sortfield (in stead of record) values $named_hash = array(); foreach($named_recs as $key=>$fields) $named_hash["$key"] = $fields[$order_by]; // Order 1-dimensional array, // maintaining key-value relations if($reverse) arsort($named_hash,$flags=0) ; else asort($named_hash, $flags=0); // Create copy of named records array // in order of sortarray $sorted_records = array(); foreach($named_hash as $key=>$val) $sorted_records["$key"]= $named_recs[$key];return $sorted_records;} // named_recs_sort()function show_sorted_records($named_recs, $order_by, $rev=false, $flags=0){$sorted_records=named_records_sort($named_recs, $order_by, $rev, $flags);foreach($sorted_records as $name=>$fields) {echo "<b>$name</b> "; foreach($fields as $field=>$val) echo "$field = $val "; echo "<br>";}} // show_sorted_records()$girl_friends=array();$girl_friends["Anna"]=array("born"=>'1989-08-22',"cupsize"=>'B-',"IQ"=>105, "daddy"=>'rich');$girl_friends["Zoe"]=array("born"=>'1978-03-11',"cupsize"=>'C#',"IQ"=>130, "daddy"=>'poor');$girl_friends["Lilly"]=array("born"=>'1985-06-16',"cupsize"=>'DD',"IQ"=>90, "daddy"=>'nasty');$order_by="cupsize"; echo "And the winners are: <br>";show_sorted_records($girl_friends, $order_by, true);