在PHP中只保留重复值数组的最低值?

时间:2015-11-03 08:19:56

标签: php arrays multidimensional-array

我有一个数组,如:

Array
(
    [0] => Array
        (
            [date] => 2015-11-01
            [price] => 95
        )

    [1] => Array
        (
            [date] => 2015-11-08
            [price] => 95
        )

    [2] => Array
        (
            [date] => 2015-11-15
            [price] => 95
        )

    [3] => Array
        (
            [date] => 2015-11-03
            [price] => 90
        )

    [4] => Array
        (
            [date] => 2015-11-04
            [price] => 90
        )

    [5] => Array
        (
            [date] => 2015-11-05
            [price] => 90
        )

    [6] => Array
        (
            [date] => 2015-11-06
            [price] => 90
        )

    [7] => Array
        (
            [date] => 2015-11-07
            [price] => 90
        )

    [8] => Array
        (
            [date] => 2015-11-08
            [price] => 90
        )

    [9] => Array
        (
            [date] => 2015-11-09
            [price] => 90
        )

    [10] => Array
        (
            [date] => 2015-11-10
            [price] => 90
        )

    [11] => Array
        (
            [date] => 2015-11-11
            [price] => 90
        )

    [12] => Array
        (
            [date] => 2015-11-12
            [price] => 90
        )

    [13] => Array
        (
            [date] => 2015-11-13
            [price] => 90
        )

    [14] => Array
        (
            [date] => 2015-11-14
            [price] => 90
        )

    [15] => Array
        (
            [date] => 2015-11-15
            [price] => 90
        )
)

我希望显示重复日期字段的唯一最低价格值。它必须是这样的。

Array(

    [0] => Array
        (
            [date] => 2015-11-01
            [price] => 95
        )

    [1] => Array
        (
            [date] => 2015-11-03
            [price] => 90
        )

    [2] => Array
        (
            [date] => 2015-11-04
            [price] => 90
        )

    [3] => Array
        (
            [date] => 2015-11-05
            [price] => 90
        )

    [4] => Array
        (
            [date] => 2015-11-06
            [price] => 90
        )

    [5] => Array
        (
            [date] => 2015-11-07
            [price] => 90
        )

    [6] => Array
        (
            [date] => 2015-11-08
            [price] => 90
        )

    [7] => Array
        (
            [date] => 2015-11-09
            [price] => 90
        )

    [8] => Array
        (
            [date] => 2015-11-10
            [price] => 90
        )

    [9] => Array
        (
            [date] => 2015-11-11
            [price] => 90
        )

    [10] => Array
        (
            [date] => 2015-11-12
            [price] => 90
        )

    [11] => Array
        (
            [date] => 2015-11-13
            [price] => 90
        )

    [12] => Array
        (
            [date] => 2015-11-14
            [price] => 90
        )

    [13] => Array
        (
            [date] => 2015-11-15
            [price] => 90
        )
)

请帮忙吗?

1 个答案:

答案 0 :(得分:2)

如果有人有同样的问题。这是我最终制作的代码..

for ($row = 0; $row < $p; $row++) {
    $id = $arr[$row]['id'];
    $date1 = $arr[$row]['date'];
    $price1 = $arr[$row]['price'];

    for ($row2 = 0; $row2 < $p; $row2++) {
        $id2 = $arr[$row2]['id'];

        if($id != $id2)
        {
            $date2 = $arr[$row2]['date'];
            $price2 = $arr[$row2]['price'];
            if($date1 == $date2)
            {
                if($price1 > $price2)
                {

                    unset($arr[$row]);
                }
            }
        }

    } }

$ arr = array_values($ arr);