过滤不在数组中工作

时间:2016-01-29 10:43:01

标签: php multidimensional-array

我正在研究旅游网站项目我想要比较两个数组,但是当我尝试在数组中添加新东西时,过滤停止。我想过滤,如果两个房间相等,那么检查它的计划并显示房间有最低费率和其他剩余结果。  我在下面写了代码

      $arr1  =   array(
    array (
                  'ratePlanCode'  => '1',
                'roomId'  => '10',
                'whotel' =>'0',
                'roomName' => 'Standard',
                'ratePlan' => 'CPAI',
                'roomRate' => 11000
            ),
      array
            (
                'ratePlanCode'  => '2',
                'roomId'  => '10',
                'whotel' =>'0',
                'roomName' => 'test',
                'ratePlan' => 'MAP',
                'roomRate' => 10000
            ),
    array
            (
                'ratePlanCode'  => '3',
                'roomId'  => '10',
                'whotel' =>'0',
                'roomName' => 'test123',
                'ratePlan' => 'CP',
                'roomRate' => 10000
            ),
    array
            (
                'ratePlanCode'  => '4',
                'roomId'  => '10',
                'whotel' =>'0',
                'roomName' => 'test',
                'ratePlan' => 'MAP',
                'roomRate' => 10000
            )        

        );


$arr2  =   array(
    array (
                'ratePlanCode'  => '10',
                'roomId'  => '10',
                'whotel' =>'1',
                'roomName' => 'Standard',
                'ratePlan' => 'CPAI',
                'roomRate' => 12000
            ),
      array
            (
                'ratePlanCode'  => '100',
                'roomId'  => '10',
                'whotel' =>'1',
                'roomName' => 'Honeymoon',
                'ratePlan' => 'MAP',
                'roomRate' => 10800
            ),

            array
            (
                'ratePlanCode'  => '102',
                'roomId'  => '10',
                'whotel' =>'1',
                'roomName' => 'test123',
                'ratePlan' => 'CP',
                'roomRate' => 9000
            ),
            array
            (
                'ratePlanCode'  => '101',
                'roomId'  => '10',
                'whotel' =>'1',
                'roomName' => 'waff',
                'ratePlan' => 'MAP',
                'roomRate' => 10800
            ));



//print_r($hotelArray);
$data123 = array_merge($arr1,$arr2);
//print_r($data);
$output = array();
foreach($data123 as $arr){
$output [$arr['roomName']][$arr['ratePlan']][$arr['ratePlanCode']][$arr['roomId']][] =$arr['roomRate'] ;
sort($output[$arr['roomName']][$arr['ratePlan']][$arr['ratePlanCode']][$arr['roomId']]);
}
print_R($output);
//deassemble
$data = array();

foreach($output as $roomName=>$arr1)
{
    foreach($arr1 as $ratePlan=>$arr2)
    {
            foreach($arr2 as $hotelId=>$arr3)
            {
                foreach($arr3 as $roomId=>$arr4)
                {
                    foreach($arr3 as $roomId=>$arr4)
                    {

                        $data[] = array(    
                        'ratePlanCode' => $hotelId,  
                        'roomId' => $roomId, 
                        'roomName' => $roomName,
                        'ratePlan' => $ratePlan,
                        'roomRate' => $arr4[0]);
                    }
                }
            }
    }
}
echo "Final Output";
print_R($data);

我正在获得这样的输出

[0] => Array
        (
            [ratePlanCode] => 1
            [roomId] => 10
            [roomName] => Standard
            [ratePlan] => CPAI
            [roomRate] => 11000
        )

    [1] => Array
        (
            [ratePlanCode] => 10
            [roomId] => 10
            [roomName] => Standard
            [ratePlan] => CPAI
            [roomRate] => 12000
        )

    [2] => Array
        (
            [ratePlanCode] => 2
            [roomId] => 10
            [roomName] => test
            [ratePlan] => MAP
            [roomRate] => 10000
        )

    [3] => Array
        (
            [ratePlanCode] => 4
            [roomId] => 10
            [roomName] => test
            [ratePlan] => MAP
            [roomRate] => 10000
        )

    [4] => Array
        (
            [ratePlanCode] => 3
            [roomId] => 10
            [roomName] => test123
            [ratePlan] => CP
            [roomRate] => 10000
        )

    [5] => Array
        (
            [ratePlanCode] => 102
            [roomId] => 10
            [roomName] => test123
            [ratePlan] => CP
            [roomRate] => 9000
        )

    [6] => Array
        (
            [ratePlanCode] => 100
            [roomId] => 10
            [roomName] => Honeymoon
            [ratePlan] => MAP
            [roomRate] => 10800
        )

    [7] => Array
        (
            [ratePlanCode] => 101
            [roomId] => 10
            [roomName] => waff
            [ratePlan] => MAP
            [roomRate] => 10800
        )

但我希望得到如下的输出你能帮我解决这个问题吗

[0] => Array
        (
            [hotelId] => 10
            [roomId] => 10
            [roomName] => Standard
            [ratePlan] => CPAI
            [roomRate] => 11000
        )

    [1] => Array
        (
            [hotelId] => 10
            [roomId] => 10
            [roomName] => test
            [ratePlan] => MAP
            [roomRate] => 10000
        )

    [2] => Array
        (
            [hotelId] => 10
            [roomId] => 10
            [roomName] => test123
            [ratePlan] => CP
            [roomRate] => 9000
        )

    [3] => Array
        (
            [hotelId] => 10
            [roomId] => 10
            [roomName] => Honeymoon
            [ratePlan] => MAP
            [roomRate] => 10800
        )

    [4] => Array
        (
            [hotelId] => 10
            [roomId] => 10
            [roomName] => waff
            [ratePlan] => MAP
            [roomRate] => 10800
        )

1 个答案:

答案 0 :(得分:1)

我在你的第一个代码块中注意到两个嵌套的foreach循环看似相同。

foreach($arr3 as $roomId=>$arr4)
            {
                foreach($arr3 as $roomId=>$arr4)
                {

                    $data[] = array(    
                    'ratePlanCode' => $hotelId,  
                    'roomId' => $roomId, 
                    'roomName' => $roomName,
                    'ratePlan' => $ratePlan,
                    'roomRate' => $arr4[0]);
                }
            }

此外,在第一次定义$ arr1和$ arr2时,以及在嵌套的foreach循环中定义$ data时,使用与输出名称完全匹配的键。我很确定这就是为什么你的输出名称不是你想要的。

接下来,您的输出包含来自$ arr1和$ arr2的数组,因为您没有过滤掉具有唯一房间的数组。我写了一个非常简单的代码片段来构建一个包含唯一roomName的新数组,按照速率和计划从$ data123按升序排序。来自SO answer的函数的代码:

$keeper = array('');
$uniquedata = array();
$data123 = array_merge($arr1,$arr2);

function DSort2($item1,$item2)
{
    if ($item1['ratePlan'] == $item2['ratePlan']) return 0;
    return ($item1['ratePlan'] > $item2['ratePlan']) ? 1 : -1;
}

usort($data123,'Dsort2');

function DSort($item1,$item2)
{
    if ($item1['roomRate'] == $item2['roomRate']) return 0;
    return ($item1['roomRate'] > $item2['roomRate']) ? 1 : -1;
}
usort($data123,'Dsort');

foreach($data123 as $array) {
if(!in_array($array['roomName'],$keeper)) {
    array_push($keeper, $array['roomName']);
    array_push($uniquedata,$array);
  }
}

接下来,我重新排序/重命名,以便输出看起来像你的:

$uniquedata = array_map(function($uniquedata) {
    return array(
        'hotelId' => $uniquedata['whotel'],
        'roomId' => $uniquedata['roomId'],
        'roomName' => $uniquedata['roomName'],
        'ratePlan' => $uniquedata['ratePlan'],
        'roomRate' => $uniquedata['roomRate']
    );
}, $uniquedata);

print_r($uniquedata);

您将在输出中找到所需的所有数组,并按正确的顺序使用正确的键和值。数组的顺序是不同的,因为在我的方法中,我通过增加速率的值来排序。您可以通过修改订购功能轻松更改此设置。祝福。