我想按价格排序这个数组,并保留我的键,而不更改。
这是我一直在做的事情。
<?php
$a = array(
1=>array('price'=>9.25,'timestamp_added'=>1301945848,'name'=>'pencils'),
4=>array('price'=>19.15,'timestamp_added'=>1299267448,'name'=>'crayon box'),
15=>array('price'=>4.25,'timestamp_added'=>1299785848,'name'=>'markers'),
2=> array('price'=>4.28,'timestamp_added'=>1299785848,'name'=>'eraser'),
44=>array('price'=>13.99,'timestamp_added'=>1299872248,'name'=>'trapper'),
32=>array('price'=>9.25,'timestamp_added'=>1299872248,'name'=>'notebook'),
14=>array('price'=>13.99,'timestamp_added'=>1301945848,'name'=>'sharpener'),
5=>array('price'=>15.01,'timestamp_added'=>1299872248,'name'=>'calculator')
);
function printList( $a ){
echo "<br />";
foreach ($a as $key => $value){
echo "<br /> Product ID $key Price: " . $value['price'] . " Timestamp: " . $value['timestamp_added'] . " Name: " . $value['name'];
}
}
printList( $a );
$price = array();
foreach ($a as $key => $row)
{
$price[$key] = $row['price'];
}
array_multisort($price, SORT_ASC, $a);
printList( $a );
?>
输出:Product ID 1 Price: 9.25 Timestamp: 1301945848 Name: pencils
Product ID 4 Price: 19.15 Timestamp: 1299267448 Name: crayon box
Product ID 15 Price: 4.25 Timestamp: 1299785848 Name: markers
Product ID 2 Price: 4.28 Timestamp: 1299785848 Name: eraser
Product ID 44 Price: 13.99 Timestamp: 1299872248 Name: trapper
Product ID 32 Price: 9.25 Timestamp: 1299872248 Name: notebook
Product ID 14 Price: 13.99 Timestamp: 1301945848 Name: sharpener
Product ID 5 Price: 15.01 Timestamp: 1299872248 Name: calculator
Product ID 0 Price: 4.25 Timestamp: 1299785848 Name: markers
Product ID 1 Price: 4.28 Timestamp: 1299785848 Name: eraser
Product ID 2 Price: 9.25 Timestamp: 1299872248 Name: notebook
Product ID 3 Price: 9.25 Timestamp: 1301945848 Name: pencils
Product ID 4 Price: 13.99 Timestamp: 1299872248 Name: trapper
Product ID 5 Price: 13.99 Timestamp: 1301945848 Name: sharpener
Product ID 6 Price: 15.01 Timestamp: 1299872248 Name: calculator
Product ID 7 Price: 19.15 Timestamp: 1299267448 Name: crayon box
对数组进行排序,但是键被更改。我怎样才能让钥匙的标签保持原样呢?
可以使用 usasort 函数
$a = array(
1=>array('price'=>9.25,'timestamp_added'=>1301945848,'name'=>'pencils'),
4=>array('price'=>19.15,'timestamp_added'=>1299267448,'name'=>'crayon box'),
15=>array('price'=>4.25,'timestamp_added'=>1299785848,'name'=>'markers'),
2=> array('price'=>4.28,'timestamp_added'=>1299785848,'name'=>'eraser'),
44=>array('price'=>13.99,'timestamp_added'=>1299872248,'name'=>'trapper'),
32=>array('price'=>9.25,'timestamp_added'=>1299872248,'name'=>'notebook'),
14=>array('price'=>13.99,'timestamp_added'=>1301945848,'name'=>'sharpener'),
5=>array('price'=>15.01,'timestamp_added'=>1299872248,'name'=>'calculator')
);
function printList( $a ){
echo "<br />";
foreach ($a as $key => $value){
echo "<br /> Product ID $key Price: " . $value['price'] . " Timestamp: " . $value['timestamp_added'] . " Name: " . $value['name'];
}
}
printList( $a );
function sbyprice($a, $b)
{
return ($a['price'] >= $b['price']) ? 1 : 0;
}
uasort($a , "sbyprice");
printList( $a );
?>
输出:
Product ID 1 Price: 9.25 Timestamp: 1301945848 Name: pencils
Product ID 4 Price: 19.15 Timestamp: 1299267448 Name: crayon box
Product ID 15 Price: 4.25 Timestamp: 1299785848 Name: markers
Product ID 2 Price: 4.28 Timestamp: 1299785848 Name: eraser
Product ID 44 Price: 13.99 Timestamp: 1299872248 Name: trapper
Product ID 32 Price: 9.25 Timestamp: 1299872248 Name: notebook
Product ID 14 Price: 13.99 Timestamp: 1301945848 Name: sharpener
Product ID 5 Price: 15.01 Timestamp: 1299872248 Name: calculator
Product ID 15 Price: 4.25 Timestamp: 1299785848 Name: markers
Product ID 2 Price: 4.28 Timestamp: 1299785848 Name: eraser
Product ID 32 Price: 9.25 Timestamp: 1299872248 Name: notebook
Product ID 1 Price: 9.25 Timestamp: 1301945848 Name: pencils
Product ID 44 Price: 13.99 Timestamp: 1299872248 Name: trapper
Product ID 14 Price: 13.99 Timestamp: 1301945848 Name: sharpener
Product ID 5 Price: 15.01 Timestamp: 1299872248 Name: calculator
Product ID 4 Price: 19.15 Timestamp: 1299267448 Name: crayon box
这是你的事
例如:<?php
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$arr=array("a"=>4,"b"=>2,"c"=>8,d=>"6");
uasort($arr,"my_sort");
?>
与其他相同,但使用内联函数,这可能更好:
uasort ( $array , function($a, $b) {
return $a['price'] >= $b['price'] ? 1 : 0 ;
});
usort将解决您的问题。