我将这个用于类似 SQL 的 orderby。松散地基于 smileaf。对你们这些极客有用吗?
<?
function named_records_sort($named_recs, $order_by, $rev=false, $flags=0)
{// 创建一个仅包含排序字段(而不是记录)值的、一维的命名数组
// 排序字段(而不是记录)值
$named_hash = array();
foreach($named_recs as $key=>$fields)
$named_hash["$key"] = $fields[$order_by];
// 对一维数组进行排序,
// 保持键值关系
if($reverse) arsort($named_hash,$flags=0) ;
else asort($named_hash, $flags=0);
// 创建命名记录数组的副本
// 按排序数组的顺序排列
$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);
?>