BeDeveloper
BeDeveloper

Reputation: 98

Merging two arrays not working as expected

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
        )

Upvotes: 0

Views: 55

Answers (1)

MathBio
MathBio

Reputation: 367

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.

Upvotes: 1

Related Questions