0

我想按价格订购这个数组并保留我的密钥而不改变它们。

这是我一直在使用的。

<?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

它对数组进行排序,但键已更改。我怎样才能保持键的原样?

4

4 回答 4

2

您可以使用uasort功能

$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
于 2013-04-17T06:57:57.230 回答
1

和其他人一样,但使用内联函数,这可能会更好:

uasort ( $array , function($a, $b) {
  return $a['price'] >= $b['price'] ? 1 : 0 ;
});
于 2013-04-17T07:10:08.340 回答
1

uasort适合你

例如:

<?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");
?> 
于 2013-04-17T06:59:42.240 回答
0

usort将解决您的问题。请参考此链接。

于 2013-04-17T06:57:27.477 回答