duanjiaopi8218 2016-01-29 10:43
浏览 11

过滤不在数组中工作

I am working on travel site project i want to compare two arrays but when i am trying add new things in the array the filtration stops.I want to filter like if two room are equal then check its plan and show room which has minimum rate and other remaining result. I wrote below code for it

      $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);

I am getting output like this

[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
        )

But i want getting output like below can you help me solve this

[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条回答 默认 最新

  • dsfw2154 2016-01-29 10:49
    关注

    I noticed in your first codeblock two nested foreach loops which appear to be identical.

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

    Further, where you first define $arr1 and $arr2, as well as when you define $data inside of the nested foreach loops, you use keys which match up exactly with your output's names. I'm pretty sure this is why you're getting output with names that aren't as you want.

    Next, your output has the arrays from both $arr1 and $arr2, since you haven't filtered out the ones with unique rooms. I wrote a very simple minded piece of code to build a new array containing unique roomName's, ordered in ascending order by rate and plan from $data123. Code from functions from 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);
      }
    }
    

    Next, I reorder/rename things so the output looks like yours:

    $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);
    

    You'll find all the arrays you desired in your output, with the correct keys and values in the correct order. The ordering of the arrays is different, since in my method I ordered by increasing value of the rate. You can easily change this by tinkering with the ordering functions. Best wishes.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集