0

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
        )
mickmackusa
  • 43,625
  • 12
  • 83
  • 136

1 Answers1

1

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.

Community
  • 1
  • 1
MathBio
  • 367
  • 1
  • 2
  • 13
  • Actually it was by mistake – BeDeveloper Jan 29 '16 at 10:54
  • @BeDeveloper I see, no worries. Just trying to help you debug. Did you see my second comment about how you define $arr1 and $arr2? – MathBio Jan 29 '16 at 11:02
  • So i have to define that at start – BeDeveloper Jan 29 '16 at 11:08
  • One option would be to try to setup your arrays differently at the start as you suggest, this is probably the simplest.. You could alternatively change the output format. For example, I don't see the hotelId variable defined until inside the foreach loop; I think you would want to change your output format to get this in your output, if you want to keep things as they are – MathBio Jan 29 '16 at 11:15
  • Not working i changed the declarations of the array but still the same result please run my code you will see the things – BeDeveloper Jan 29 '16 at 11:17
  • Please check my expected output it not matches withyours – BeDeveloper Jan 30 '16 at 09:39
  • Okay. So the code above will output all the arrays you asked for, with the key-value pairs in the correct indexing. I noticed your hotelId's don't correspond with the 'whotel' data in the arrays, though in my output the hotelId's are correct. If you want all the hotelId's to be 10, you can just set them = 10 instead of the value in 'whotel'. My arrays print in a different order, since they're ordered from least rate to highest rate; that's the only way they differ from your output. I hope this suffices. Best wishes. – MathBio Jan 30 '16 at 16:06