php - Filteration Not Working in array -


i working on travel site project want compare 2 arrays when trying add new things in array filtration stops.i want filter if 2 room equal check plan , show room has minimum rate , other remaining result. wrote below code 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 $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 $roomname=>$arr1) {     foreach($arr1 $rateplan=>$arr2)     {             foreach($arr2 $hotelid=>$arr3)             {                 foreach($arr3 $roomid=>$arr4)                 {                     foreach($arr3 $roomid=>$arr4)                     {                          $data[] = array(                             'rateplancode' => $hotelid,                           'roomid' => $roomid,                          'roomname' => $roomname,                         'rateplan' => $rateplan,                         'roomrate' => $arr4[0]);                     }                 }             }     } } echo "final output"; print_r($data); 

i getting output

[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 want getting output below can 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         ) 

i noticed in first codeblock 2 nested foreach loops appear identical.

foreach($arr3 $roomid=>$arr4)             {                 foreach($arr3 $roomid=>$arr4)                 {                      $data[] = array(                         'rateplancode' => $hotelid,                       'roomid' => $roomid,                      'roomname' => $roomname,                     'rateplan' => $rateplan,                     'roomrate' => $arr4[0]);                 }             } 

further, first define $arr1 , $arr2, when define $data inside of nested foreach loops, use keys match output's names. i'm pretty sure why you're getting output names aren't want.

next, output has arrays both $arr1 , $arr2, since haven't filtered out ones unique rooms. wrote simple minded piece of code build new array containing unique roomname's, ordered in ascending order rate , plan $data123. code functions 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 $array) { if(!in_array($array['roomname'],$keeper)) {     array_push($keeper, $array['roomname']);     array_push($uniquedata,$array);   } } 

next, reorder/rename things output looks 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 arrays desired in output, correct keys , values in correct order. ordering of arrays different, since in method ordered increasing value of rate. can change tinkering ordering functions. best wishes.


Comments