【问题标题】:Ordering multidimensional associative arrays排序多维关联数组
【发布时间】:2013-04-09 19:31:16
【问题描述】:

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

这是我一直在使用的。

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

它对数组进行排序,但键已更改。如何保持键的原样?

【问题讨论】:

  • 有一个 usort() 函数
  • 谢谢,但我不明白如何将它与多维关联数组一起使用。

标签: php multidimensional-array key associative-array array-multisort


【解决方案1】:

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

uasort ( $array , function($a, $b) {
  return $a['price'] >= $b['price'] ? 1 : 0 ;
});

【讨论】:

    【解决方案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
    

    【讨论】:

      【解决方案3】:

      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");
      ?> 
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 我认为,OP 需要 uasort,因为应该维护索引。
        猜你喜欢
        • 1970-01-01
        • 2011-12-20
        • 1970-01-01
        • 2012-12-17
        • 1970-01-01
        • 2020-03-05
        • 2019-04-02
        • 1970-01-01
        • 2017-12-01
        相关资源
        最近更新 更多