如何通过值比较对数字数组进行排序并更改数组索引的位置

时间:2019-01-01 18:15:37

标签: php arrays sorting

我需要在某种条件下对数组进行排序。

Array
(
    [0] => Array
        (
            [entity_id] => 10
            [product_id] => 424
            [city] => 18
            [sort_order] => 10
            [deal_type_id] => dow
            [deal_from_date] => 2010-12-18
            [deal_to_date] => 2026-02-19
        )

    [1] => Array
        (
            [entity_id] => 11
            [product_id] => 423
            [city] => 18
            [sort_order] => 11
            [deal_type_id] => dow
            [deal_from_date] => 2010-12-18
            [deal_to_date] => 2027-02-19
        )

    [2] => Array
        (
            [entity_id] => 12
            [product_id] => 422
            [city] => 18
            [sort_order] => 12
            [deal_type_id] => dow
            [deal_from_date] => 2011-12-18
            [deal_to_date] => 2030-01-19
        )

    [3] => Array
        (
            [entity_id] => 13
            [product_id] => 420
            [city] => 18
            [sort_order] => 13
            [deal_type_id] => dow
            [deal_from_date] => 2011-12-18
            [deal_to_date] => 2030-01-19
        )

    [4] => Array
        (
            [entity_id] => 1
            [product_id] => 463
            [city] => 18
            [sort_order] => 1
            [deal_type_id] => dod
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2025-01-19
        )

    [5] => Array
        (
            [entity_id] => 2
            [product_id] => 452
            [city] => 18
            [sort_order] => 2
            [deal_type_id] => dom
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2026-01-19
        )

    [6] => Array
        (
            [entity_id] => 3
            [product_id] => 448
            [city] => 18
            [sort_order] => 3
            [deal_type_id] => dod
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2027-01-19
        )

    [7] => Array
        (
            [entity_id] => 4
            [product_id] => 441
            [city] => 18
            [sort_order] => 4
            [deal_type_id] => dod
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2024-01-19
        )

    [8] => Array
        (
            [entity_id] => 5
            [product_id] => 440
            [city] => 18
            [sort_order] => 5
            [deal_type_id] => dod
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2027-01-19
        )

    [9] => Array
        (
            [entity_id] => 6
            [product_id] => 439
            [city] => 18
            [sort_order] => 6
            [deal_type_id] => dom
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2028-01-19
        )

    [10] => Array
        (
            [entity_id] => 7
            [product_id] => 433
            [city] => 18
            [sort_order] => 7
            [deal_type_id] => dom
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2013-01-19
        )

    [11] => Array
        (
            [entity_id] => 8
            [product_id] => 427
            [city] => 18
            [sort_order] => 8
            [deal_type_id] => dod
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2022-01-19
        )

    [12] => Array
        (
            [entity_id] => 9
            [product_id] => 425
            [city] => 18
            [sort_order] => 9
            [deal_type_id] => dom
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2025-02-19
        )

)

我需要比较上一个键和下一个键的deal_from_date。 如果两者相同,那么我需要检查deal_to_date中的刨丝器值。 如果我发现两个deal_to_date的值都不同,则按更大的顺序排序。

我尝试过。

for($i=0;$i<count($result);$i++)
{
    if(array_key_exists($i+1, $result))
    {
        if(strtotime($result[$i]['deal_from_date']) == strtotime($result[$i+1]['deal_from_date']))
        {
            echo $i;
            if(strtotime($result[$i]['deal_to_date']) < strtotime($result[$i+1]['deal_to_date']))
            { 
                $firstArray = $result[$i];
                unset($result[$i]);
                $secondArray = $result[$i+1];
                unset($result[$i]);
                $result[$i] = $secondArray;
                $result[$i+1] = $firstArray;
            }
        }
    }
}

我需要此输出。

Array
(

    [0] => Array
    (
        [entity_id] => 11
        [product_id] => 423
        [city] => 18
        [sort_order] => 11
        [deal_type_id] => dow
        [deal_from_date] => 2010-12-18
        [deal_to_date] => 2027-02-19
    )
    [1] => Array
        (
            [entity_id] => 10
            [product_id] => 424
            [city] => 18
            [sort_order] => 10
            [deal_type_id] => dow
            [deal_from_date] => 2010-12-18
            [deal_to_date] => 2026-02-19
        )

   [1] => Array
        (
            [entity_id] => 12
            [product_id] => 422
            [city] => 18
            [sort_order] => 12
            [deal_type_id] => dow
            [deal_from_date] => 2011-12-18
            [deal_to_date] => 2030-01-19
        )

    [2] => Array
        (
            [entity_id] => 13
            [product_id] => 420
            [city] => 18
            [sort_order] => 13
            [deal_type_id] => dow
            [deal_from_date] => 2011-12-18
            [deal_to_date] => 2030-01-19
        )

    [3] => Array
        (
            [entity_id] => 2
            [product_id] => 452
            [city] => 18
            [sort_order] => 2
            [deal_type_id] => dom
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2026-01-19
        )
    [4] => Array
        (
            [entity_id] => 1
            [product_id] => 463
            [city] => 18
            [sort_order] => 1
            [deal_type_id] => dod
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2025-01-19
        )



    [5] => Array
        (
            [entity_id] => 3
            [product_id] => 448
            [city] => 18
            [sort_order] => 3
            [deal_type_id] => dod
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2027-01-19
        )

       [6] => Array
        (
            [entity_id] => 6
            [product_id] => 439
            [city] => 18
            [sort_order] => 6
            [deal_type_id] => dom
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2028-01-19
        )
    [7] => Array
        (
            [entity_id] => 5
            [product_id] => 440
            [city] => 18
            [sort_order] => 5
            [deal_type_id] => dod
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2027-01-19
        )

[8] => Array
        (
            [entity_id] => 9
            [product_id] => 425
            [city] => 18
            [sort_order] => 9
            [deal_type_id] => dom
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2025-02-19
        )
    [9] => Array
        (
            [entity_id] => 4
            [product_id] => 441
            [city] => 18
            [sort_order] => 4
            [deal_type_id] => dod
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2024-01-19
        )


  [10] => Array
        (
            [entity_id] => 8
            [product_id] => 427
            [city] => 18
            [sort_order] => 8
            [deal_type_id] => dod
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2022-01-19
        )

    [11] => Array
        (
            [entity_id] => 7
            [product_id] => 433
            [city] => 18
            [sort_order] => 7
            [deal_type_id] => dom
            [deal_from_date] => 2013-12-18
            [deal_to_date] => 2013-01-19
        )





)

如果从日期开始的多个交易是同一日期,我需要对数组进行排序。 然后根据Deal_to_date desc进行排序。仅适用于同一组。

2 个答案:

答案 0 :(得分:0)

只需使用usort()函数,如下所示:

function sorter($a,$b){return $a["deal_from_date"]<=>$b["deal_from_date"]}
usort($array,sorter)

我希望这会对您有所帮助!

答案 1 :(得分:0)

如果我的理解正确,则您尝试先将deal_from_date升序进行排序,如果它们相等,则将deal_to_date降序进行排序。

您可以使用usort做这样的事情:

usort($array, function (array $entry1, array $entry2) {
  return new \DateTime($entry1['deal_from_date']) <=> new \DateTime($entry2['deal_from_date'])
    ?: (new \DateTime($entry2['deal_to_date']) <=> new \DateTime($entry1['deal_to_date']));
});

此处演示:https://3v4l.org/esc9Q

您可以添加任意数量的?: (value1 <=> value2)进行后备排序。

现在看来似乎无法产生您发布的确切输出,但是您应该可以轻松地从那里进行调整。