如何在具有相同的值的阵列结合? [英] How to combine arrays that have the same value?
本文介绍了如何在具有相同的值的阵列结合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数组是这样的:
$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的结果是这样的:
I want to combine arrays that have the same value. So the result of print_r is like this :
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
)
)
)
我不得不设法解决我的问题,但我仍然困惑
I had try to solve my problem, but I'm still confused
我只能code是这样的:
I can only make code like this :
$tmp = array();
foreach($arr as $arg)
{
$tmp['description'][]['hotel_description'] = $arg['hotel_description'];
}
任何解决方案来解决我的问题?
Any solution to solve my problem?
非常感谢你。
推荐答案
试试这个:它很容易和工作...
Try this : its easy and working...
<?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>";
?>
OUTPUT:
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
)
)
)
这篇关于如何在具有相同的值的阵列结合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文