我想按价格订购这个数组并保留我的密钥而不改变它们。
这是我一直在使用的。
<?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
它对数组进行排序,但键已更改。我怎样才能保持键的原样?