我将此用于准SQL orderby。松散地基于smileaf。对你们这些极客有用吗?
<?
函数 named_records_sort($named_recs, $order_by, $rev=false, $flags=0)
{// 创建仅包含
// 排序字段(而不是记录)值的单维命名数组
$named_hash = array();
循环遍历($named_recs 作为 $key=>$fields)
$named_hash["$key"] = $fields[$order_by];
// 对一维数组进行排序,
// 保持键值关系
如果($reverse) arsort($named_hash,$flags=0) ;
否则 asort($named_hash, $flags=0);
// 创建命名记录数组的副本
// 按排序数组的顺序
$sorted_records = array();
循环遍历($named_hash 作为 $key=>$val)
$sorted_records["$key"]= $named_recs[$key];
返回 $sorted_records;} // named_recs_sort()
函数 show_sorted_records($named_recs, $order_by, $rev=false, $flags=0)
{$sorted_records=named_records_sort($named_recs, $order_by, $rev, $flags);
循环遍历($sorted_records 作为 $name=>$fields)
{echo "<b>$name</b> ";
循环遍历($fields 作为 $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 "获胜者是: <br>";
show_sorted_records($girl_friends, $order_by, true);
?>