1

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
        )

As i have searched but it is but not working

varad mayee
  • 619
  • 7
  • 19

2 Answers2

0

function array_multisort() is useful,

   <?php
    $array = array('Alpha', 'atomic', 'Beta', 'bank');
    $array_lowercase = array_map('strtolower', $array);

    array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $array);

    print_r($array);
    ?>
Vasim Shaikh
  • 4,485
  • 2
  • 23
  • 52
0

Here is your answer :)

$mergedArr = array_merge($arr1,$arr2);
$res = [];
foreach($mergedArr as $key=>$val){      
        $res[$val['roomRate']] = $val['roomName'];  
}
//echo '<pre>'; print_R($res);
ksort($res);
$res = array_unique($res);
//echo '<pre>'; print_R($res);
//echo '<pre>'; print_R();
$resultArrFinal = [];
foreach($mergedArr as $key=>$val){
    foreach($res as $k=>$v){
        if(in_array($k,$val) && in_array($v,$val)){
            $resultArrFinal[] = $val;       
        }
    }
 //echo $val['roomRate'].'   '.$val['roomName'];
}

echo '<pre>'; print_R($resultArrFinal);

if you want completely unique records then add this line also at end.

function super_unique($array,$key)
{
   $temp_array = array();
   foreach ($array as &$v) {
       if (!isset($temp_array[$v[$key]]))
       $temp_array[$v[$key]] =& $v;
   }
   $array = array_values($temp_array);
   return $array;
}

echo '<pre>'; print_R(super_unique($resultArrFinal,'roomName'));

Hope it will help you :)

Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
  • It is not comparing the record properly.The logic behind comparing that we have compare first room name if they are matched then check its room plan if they are matched then check for there rate the room with minimum rate we have to show plus other remaining rooms in array1 and array2.Please check your output and my output – varad mayee Jan 30 '16 at 04:19