我正在研究旅游网站项目我想要比较两个数组,但是当我尝试在数组中添加新东西时,过滤停止。我想过滤,如果两个房间相等,那么检查它的计划并显示房间有最低费率和其他剩余结果。我在下面写了代码
$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);
我正在获得这样的输出
[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
)
但我希望得到如下的输出你能帮我解决这个问题吗
[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
)
正如我已经搜索但它不起作用
答案 0 :(得分:0)
函数array_multisort()非常有用,
<?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);
?>
答案 1 :(得分:0)
这是你的答案:)
$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);
如果你想要完全独特的记录,那么也在最后添加这一行。
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'));
希望它会对你有所帮助:)。