【问题标题】:Sort array of arrays by value [duplicate]按值对数组进行排序[重复]
【发布时间】:2013-07-26 07:28:30
【问题描述】:

我有一个这样的数组:

Array(
    [id1] => Array (
           /* details */
           [unique] => 3
          )
    [id2] => Array (
           /* details */
           [unique] => 5
    [id3] => Array (
           /* details */
           [unique] => 1
    )
)

问题:如何按unique 字段对其进行排序,以便像下面那样转换?

Array(
    [id2] => Array (
           /* details */
           [unique] => 5
          )
    [id1] => Array (
           /* details */
           [unique] => 3
    [id3] => Array (
           /* details */
           [unique] => 1
    )
)

【问题讨论】:

标签: php arrays sorting


【解决方案1】:

usort 函数的经典任务:

<?php
$data = array(
    "id1" => Array (
        "unique" => 3
        ),
    "id2" => Array (
        "unique" => 5),
    "id3" => Array (
        "unique" => 1
        ),
    );

function cmp($a, $b)
// {{{
{
    if ($a["unique"] == $b["unique"]) {
        return 0;
    }
    return ($a["unique"] < $b["unique"]) ? 1 : -1;
}
// }}}

usort($data, "cmp");
var_dump($data);

打印:

array(3) {
  [0]=>
  array(1) {
    ["unique"]=>
    int(5)
  }
  [1]=>
  array(1) {
    ["unique"]=>
    int(3)
  }
  [2]=>
  array(1) {
    ["unique"]=>
    int(1)
  }
}

http://www.php.net/manual/en/function.usort.php

【讨论】:

    【解决方案2】:

    使用我的自定义函数可以对数组进行排序

    function multisort (&$array, $key) {
        $sorter=array();
        $ret=array();
        reset($array);
        foreach ($array as $ii => $va) {
            $sorter[$ii]=$va[$key];
        }
        asort($sorter);
        foreach ($sorter as $ii => $va) {
            $ret[$ii]=$array[$ii];
        }
        $array=$ret;
    }
    
    multisort ($your_array,"order");
    

    【讨论】:

      【解决方案3】:

      通过使用usort()

      $array = array(
          "id1" => array("unique" => 3),
          "id2" => array("unique" => 5),
          "id3" => array("unique" => 1),
      );
      
      usort($array, function ($a, $b){
          if ($a['unique'] == $b) {
              return 0;
          }
          return ($a['unique'] > $b['unique']) ? -1 : 1;
      });
      
      print_r($array);
      

      输出:

      Array
      (
          [0] => Array
              (
                  [unique] => 5
              )
          [1] => Array
              (
                  [unique] => 3
              )
          [2] => Array
              (
                  [unique] => 1
              )
      )
      

      【讨论】:

        【解决方案4】:

        我觉得我在重复自己,对不起:D

        不要犹豫,从框架中查看库。

        [CakePHP] 制作了一个很棒的类,它能够使用点语法符号中的字符串导航到数组中。这个库被称为Hash,看看它。

        方法Sort 用于根据您在“路径”属性中放置的内容对具有键和值的数组进行排序。 它比其他函数更有用,因为它通过保留索引和该数组中包含的所有其他子值(包括更多维的子数组)对数组进行排序。

        你的例子:

        $array = array(
            'id1' => array (
                'unique' => 3,
            ),
            'id2' => array (
                'unique' => 5,
            ),
            'id3' => Array (
                'unique' => 1,
            ),
        );
        

        就这样写吧:

        /**
         * First param: the array
         * Second param : the "path" of the datas to save.
         *  "{s}" means "Matching any kind of index", for you to sort ALL indexes matching
         *  the dot "." is used to navigate into the array like an object
         *  And "unique" is the key which you want to be sorted by its value
         * The third param is simply the order method, "ascending" or "descending"
        */
        $sorted_array = Hash::sort($array, '{s}.unique', 'asc');
        

        $sorted_array 将等于这个:

        array (
            'id3' => array (
                'unique' => 1,
            ),
            'id1' => array (
                'unique' => 3,
            ),
            'id2' => array (
                'unique' => 5,
            ),
        );
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-12-02
          • 2021-11-14
          • 1970-01-01
          • 2011-12-02
          • 2018-02-01
          • 2021-05-07
          • 1970-01-01
          相关资源
          最近更新 更多