在子数组中按多个值对PHP数组进行排序

时间:2011-11-03 15:49:16

标签: php arrays sorting usort

所以我不确定这个标题是否最合适,但这就是数组的样子:

array (
   [0] => array (
              [category] => 'Value_1'
              [date]     => '01/01/2011'
              [data]     => 'A'
          )
   [1] => array (
              [category] => 'Value_3'
              [date]     => '01/01/2000'
              [data]     => 'B'
          )
   [2] => array (
              [category] => 'Value_2'
              [date]     => '01/01/2011'
              [data]     => 'D'
          )
   [3] => array (
              [category] => 'Value_2'
              [date]     => '01/01/2010'
              [data]     => 'A'
          )
   [4] => array (
              [category] => 'Value_2'
              [date]     => '01/01/2011'
              [data]     => 'C'
          )
)

我希望如何对这些数据进行排序:

  1. 保留类别的顺序
  2. 在类别中,按日期排序DESC
  3. 如果日期多次出现,请按字母顺序排序ASC
  4. 然后,示例数组将排序为array ([0], [1], [4], [2], [3]),更具体地说:

    array (
       [0] => array (
                  [category] => 'Value_1'
                  [date]     => '01/01/2011'
                  [data]     => 'A'
              )
       [1] => array (
                  [category] => 'Value_3'
                  [date]     => '01/01/2000'
                  [data]     => 'B'
              )
       [2] => array (
                  [category] => 'Value_2'
                  [date]     => '01/01/2011'
                  [data]     => 'C'
              )
       [3] => array (
                  [category] => 'Value_2'
                  [date]     => '01/01/2011'
                  [data]     => 'D'
              )
       [4] => array (
                  [category] => 'Value_2'
                  [date]     => '01/01/2010'
                  [data]     => 'A'
              )
    )
    

    我的问题是我知道我需要usort和/或array_multisort(),但我不确定如何有效地迭代循环以便按照我给出的标准进行排序

2 个答案:

答案 0 :(得分:3)

使用一个很好的比较函数uasort()usort()可以很容易地解决您的问题。以下是它的工作原理:

您的比较函数接受两个参数,这些参数是您排序的数组的元素。如果第一个参数应该首先出现在排序数组中,则返回-1;如果第一个参数应该首先出现,则返回1;如果在排序顺序中认为两个参数都相等,则返回0。

您的订单标准如下:按类别排序。如果类别相同,则按日期排序。如果日期相同,请按数据排序。

不幸的是,您的阵列结构不正确。如果另一个category = Value_3作为数组中的最后一个元素出现,该怎么办?它应该与其他Value_3条目分组,还是应该单独排序?根据这个答案,应该重新构建数组以便于排序。

另一项改进是存储日期的方式。美国日期格式完全不能用于排序,既不是字符串也不是数字值。将其转换为unix时间戳,或使用ISO日期格式“YYYY-MM-DD”。两者都可以毫不费力地进行比较。

答案 1 :(得分:1)

假设你的数组在$data变量中,试试这个:

$data = Array(
    0 => array(
        "category" => 'Value_1',
        "date" => '01/01/2011',
        "data" => 'A'
    ),
    1 => array(
        "category" => 'Value_3',
        "date" => '01/01/2000',
        "data" => 'B'
    ),
    2 => array(
        "category" => 'Value_2',
        "date" => '01/01/2011',
        "data" => 'D'
    ),
    3 => array(
        "category" => 'Value_2',
        "date" => '01/01/2010',
        "data" => 'A'
    ),
    4 => array(
        "category" => 'Value_2',
        "date" => '01/01/2011',
        "data" => 'C'
    )
);

$sorted = false;
foreach ($data as $index => $row) {
    $data[$index]['date'] = strtotime($data[$index]['date']);
}
while (!$sorted) {
    $aux = null;
    $prevCat = null;
    $prevDate = null;
    $prevData = null;
    foreach ($data as $index => $row) {
        if ($prevCat != $row['category']) {
            $prevCat = $row['category'];
            $prevDate = $row['date'];
            $prevData = $row['data'];
            continue;
        } else {
            if ($row['date'] > $prevDate) {
                $sorted = false;
                $aux = $data[$index - 1];
                $data[$index - 1] = $row;
                $data[$index] = $aux;
                break;
            }
            if ($row['date'] == $prevDate && $row['data'] < $prevData) {
                $sorted = false;
                $aux = $data[$index - 1];
                $data[$index - 1] = $row;
                $data[$index] = $aux;
                break;
            }
            $prevCat = $row['category'];
            $prevDate = $row['date'];
            $prevData = $row['data'];
        }
    }
    $sorted = ($aux == null);
}
foreach ($data as $index => $row)
    $data[$index]['date'] = date("m/d/Y", $data[$index]['date']);
var_dump($data);

//输出

array(5) {
    [0] => array(3) {
        ["category"] => string(7) "Value_1"
        ["date"] => string(10) "01/01/2011                                                               
        ["data"] => string(1) "A"
    }
    [1] => array(3) {
        ["category"] => string(7) "Value_3"
        ["date"] => string(10) "01/01/2000"
        ["data"] => string(1) "B"
    }
    [2] => array(3) {
        ["category"] => string(7) "Value_2"
        ["date"] => string(10) "01/01/2011"
        ["data"] => string(1) "C"
    }
    [3] => array(3) {
        ["category"] => string(7) "Value_2"
        ["date"] => string(10) "01/01/2011"
        ["data"] => string(1) "D"
    }
    [4] => array(3) {
        ["category"] => string(7) "Value_2"
        ["date"] => string(10) "01/01/2010"
        ["data"] => string(1) "A"
    }
}