PHP Merge 2多维数组删除重复项

时间:2016-06-14 07:49:37

标签: php arrays multidimensional-array

我有2个带有Flightdates(价格,日期,航空公司......)的多维数组。我想合并并删除具有相同日期的重复项,但我需要保持更便宜的。我总是一样的航班,但价格不同。

function gestion_lien_consigne()
{
    var value = $("select#profil_id").val();
    if(value  == "1")
        window.open('http://google.com');
    else
      window.open('http://yahoo.com');
}   

1 个答案:

答案 0 :(得分:0)

只需遍历第一个数组并匹配第二个数组中第一个数组的键的价格,然后将值推入新数组中。

第一个答案

<?php
$newArray = array();
foreach($array1 as $key =>$val)
{
    if($val['price'] <= $array2[$key]['price'])
    {
        $newArray[] = $val;
    }
    else
    {
        $newArray[] = $array2[$key];
    }
}
print_r($newArray);
?>

现场示例:CLICK HERE

第二次回答

$newArray = array();
foreach($array1 as $key => $val)
{
    foreach($array2 as $k => $v)
    {
        if($val['date1'] == $v['date1'] && $val['date2'] == $v['date2'] && $val['airline'] ==  $v['airline'])
        {
            if($val['price'] <= $array2[$key]['price'])
            {
                $newArray[] = $val;
            }
            else
            {
                $newArray[] = $v;
            }
        }
    }
}
print_r($newArray);

现场示例:CLICK HERE

这会给你:

Array
(
    [0] => Array
        (
            [price] => 191
            [date1] => 22-07-2016
            [date2] => 30-07-2016
            [airline] => Lufthansa
        )

    [1] => Array
        (
            [price] => 80
            [date1] => 22-07-2016
            [date2] => 30-07-2016
            [airline] => Easyjet
        )

    [2] => Array
        (
            [price] => 200
            [date1] => 22-07-2016
            [date2] => 30-07-2016
            [airline] => Airberlin
        )

)