我的数组是这样的:
$arr = array(
array(
'id' => 1,
'hotel_code' => 'PHCEB_00001',
'hotel_name' => 'Cebu Hotel',
'address' => 'Cebu Address',
'hotel_title' => 'LOCATION DETAIL',
'hotel_description' => '7 kms to city centre 2 kms to the airport 60 kms to the airport 6 km to the nearest station ',
),
array(
'id' => 1,
'hotel_code' => 'PHCEB_00001',
'hotel_name' => 'Cebu Hotel',
'address' => 'Cebu Address',
'hotel_title' => 'ROOMS',
'hotel_description' => 'Rooms with elegant set up viewing our beautiful natural landscape garden, private balcony'
),
array(
'id' => 1,
'hotel_code' => 'PHCEB_00001',
'hotel_name' => 'Cebu Hotel',
'address' => 'Cebu Address',
'hotel_title=> 'RESTAURANT',
'hotel_description' => 'The Restaurant serving Western cuisines to cater your appetite and open for 24 hours'
),
);
我想组合具有相同值的数组。所以print_r的结果是这样的:
Array
(
[id] => 1
[hotel_code] => PHCEB_00001
[hotel_name] => Cebu Hotel
[address] => Cebu Address
[description] => Array
(
[0] => Array
(
[hotel_title] => LOCATION DETAIL
[hotel_description] => 7 kms to city centre 2 kms to the airport 60 kms to the airport 6 km to the nearest station
)
[1] => Array
(
[hotel_title] => ROOMS
[hotel_description] => Rooms with elegant set up viewing our beautiful natural landscape garden, private balcony
)
[2] => Array
(
[hotel_title] => RESTAURANT
[hotel_description] => The Restaurant serving Western cuisines to cater your appetite and open for 24 hours
)
)
)
我试图解决我的问题,但我仍然感到困惑
我只能制作这样的代码:
$tmp = array();
foreach($arr as $arg)
{
$tmp['description'][]['hotel_description'] = $arg['hotel_description'];
}
解决我问题的任何解决方案?
非常感谢
答案 0 :(得分:0)
当有“酒店”以及随机顺序中不同的“ id ”字段时,我已经复杂了您的初始数组以显示复杂情况。
使用uasort
,array_reduce
和array_diff
函数的解决方案:
$arr = array(
array(
'id' => 1,
'hotel_code' => 'PHCEB_00001',
'hotel_name' => 'Cebu Hotel',
'address' => 'Cebu Address',
'hotel_title' => 'LOCATION DETAIL',
'hotel_description' => '7 kms to city centre 2 kms to the airport 60 kms to the airport 6 km to the nearest station ',
),
array(
'id' => 2,
'hotel_code' => 'PHCEB_00001',
'hotel_name' => 'Cebu Hotel',
'address' => 'Cebu Address',
'hotel_title' => 'test',
'hotel_description' => 'Test'
),
array(
'id' => 1,
'hotel_code' => 'PHCEB_00001',
'hotel_name' => 'Cebu Hotel',
'address' => 'Cebu Address',
'hotel_title'=> 'RESTAURANT',
'hotel_description' => 'The Restaurant serving Western cuisines to cater your appetite and open for 24 hours'
),
array(
'id' => 1,
'hotel_code' => 'PHCEB_00001',
'hotel_name' => 'Cebu Hotel',
'address' => 'Cebu Address',
'hotel_title' => 'ROOMS',
'hotel_description' => 'Rooms with elegant set up viewing our beautiful natural landscape garden, private balcony'
),
);
/********** Solution ***********/
uasort($arr, function($a,$b){
return ($a['id'] < $b['id'])? -1 : (($a['id'] > $b['id'])? 1 : 0);
});
$result = [];
array_reduce($arr, function($prev, $item) use (&$result){
if( empty(array_diff([$prev['id'], $prev['hotel_code'], $prev['hotel_name'], $prev['address']], [$item['id'], $item['hotel_code'], $item['hotel_name'], $item['address']])) ){
if (!isset($result[$prev['id']])){
$result[$prev['id']] = [
'id' => $prev['id'],
'hotel_code' => $prev['hotel_code'],
'hotel_name' => $prev['hotel_name'],
'address' => $prev['address'],
'description' => [
['hotel_title' => $prev['hotel_title'], 'hotel_description' => $prev['hotel_description']],
['hotel_title' => $item['hotel_title'], 'hotel_description' => $item['hotel_description']],
]
];
} else {
$result[$prev['id']]['description'][] = ['hotel_title' => $item['hotel_title'], 'hotel_description' => $item['hotel_description']];
}
}
return $item;
});
/*********************/
var_dump($result);
// the output:
array (size=1)
1 =>
array (size=5)
'id' => int 1
'hotel_code' => string 'PHCEB_00001' (length=11)
'hotel_name' => string 'Cebu Hotel' (length=10)
'address' => string 'Cebu Address' (length=12)
'description' =>
array (size=3)
0 =>
array (size=2)
'hotel_title' => string 'RESTAURANT' (length=10)
'hotel_description' => string 'The Restaurant serving Western cuisines to cater your appetite and open for 24 hours' (length=84)
1 =>
array (size=2)
'hotel_title' => string 'LOCATION DETAIL' (length=15)
'hotel_description' => string '7 kms to city centre 2 kms to the airport 60 kms to the airport 6 km to the nearest station ' (length=92)
2 =>
array (size=2)
'hotel_title' => string 'ROOMS' (length=5)
'hotel_description' => string 'Rooms with elegant set up viewing our beautiful natural landscape garden, private balcony' (length=89)
答案 1 :(得分:0)
试试这个:它简单易用......
<?php
$arr = array(
array(
'id' => 1,
'hotel_code' => 'PHCEB_00001',
'hotel_name' => 'Cebu Hotel',
'address' => 'Cebu Address',
'hotel_title' => 'LOCATION DETAIL',
'hotel_description' => '7 kms to city centre 2 kms to the airport 60 kms to the airport 6 km to the nearest station ',
),
array(
'id' => 1,
'hotel_code' => 'PHCEB_00001',
'hotel_name' => 'Cebu Hotel',
'address' => 'Cebu Address',
'hotel_title' => 'ROOMS',
'hotel_description' => 'Rooms with elegant set up viewing our beautiful natural landscape garden, private balcony'
),
array(
'id' => 1,
'hotel_code' => 'PHCEB_00001',
'hotel_name' => 'Cebu Hotel',
'address' => 'Cebu Address',
'hotel_title' => 'RESTAURANT',
'hotel_description' => 'The Restaurant serving Western cuisines to cater your appetite and open for 24 hours'
)
);
$intersect = array_intersect($arr[0], $arr[1], $arr[2]);
$intersectkeys = array_keys($intersect);
foreach ($arr as $key1 => $value1) {
foreach ($value1 as $key2 => $value2) {
if(in_array($key2, $intersectkeys)){
unset($arr[$key1][$key2]);
}
}
}
$newarr['description'] = $arr;
$result = array_merge($intersect, $newarr);
echo "<pre>".print_r($result,1)."</pre>";
?>
输出:
Array
(
[id] => 1
[hotel_code] => PHCEB_00001
[hotel_name] => Cebu Hotel
[address] => Cebu Address
[description] => Array
(
[0] => Array
(
[hotel_title] => LOCATION DETAIL
[hotel_description] => 7 kms to city centre 2 kms to the airport 60 kms to the airport 6 km to the nearest station
)
[1] => Array
(
[hotel_title] => ROOMS
[hotel_description] => Rooms with elegant set up viewing our beautiful natural landscape garden, private balcony
)
[2] => Array
(
[hotel_title] => RESTAURANT
[hotel_description] => The Restaurant serving Western cuisines to cater your appetite and open for 24 hours
)
)
)