所以我有一个像这样的数组:-
$headers = array();
$headers[] = array("country" => "Netherlands", "city" => "Amsterdam");
$headers[] = array("country" => "Netherlands", "city" => "Tillsburg");
$headers[] = array("country" => "Sweden", "city" => "Stockholm");
我需要它:-
array("country" => "Netherlands", "city" => "Amsterdam,Tillsburg");
array("country" => "Sweden", "city" => "Stockholm");
如何合并值?
这是我到目前为止尝试过的方法,但是似乎没有用。
function merge($headers) {
$final = [];
foreach($headers as $current) {
if(!in_array($current['country'], $final)) {
$final[] = $current['country'];
}
}
foreach($headers as $current) {
$final[$current['country']]['city'] = $current['city'];
}
return $final;
}
任何帮助将不胜感激。
答案 0 :(得分:0)
也许这不是最好的解决方案,但我对其进行了测试,效果很好。
function merge($headers) {
$final = array();
foreach ($headers as $current) {
if( !isset( $final[ $current['country'] ] ) ) {
$final[$current['country']] = [
'country' => $current['country'],
'city' => $current['city']
];
continue;
}
$final[ $current['country'] ]['city'] = $final[ $current['country'] ]['city'] . ', ' . $current['city'];
}
return $final;
}
输出
array(2) { ["Netherlands"]=> array(2) { ["country"]=> string(11) "Netherlands" ["city"]=> string(20) "Amsterdam, Tillsburg" } ["Sweden"]=> array(2) { ["country"]=> string(6) "Sweden" ["city"]=> string(9) "Stockholm" } }
如果要添加,可以清除结果数组的键
$final = array_values($final);
在return
语句之前
答案 1 :(得分:0)
使用国家/地区值作为临时关联键,以确定是否有必要进行串联。
代码:(Demo)
$headers[] = array("country" => "Netherlands", "city" => "Amsterdam");
$headers[] = array("country" => "Netherlands", "city" => "Tillsburg");
$headers[] = array("country" => "Sweden", "city" => "Stockholm");
foreach ($headers as $row) {
if (!isset($result[$row['country']])) {
$result[$row['country']] = $row; // store the whole row
} else {
$result[$row['country']]['city'] .= ",{$row['city']}"; // concatenate
}
}
var_export(array_values($result)); // reindex the output array
输出:
array (
0 =>
array (
'country' => 'Netherlands',
'city' => 'Amsterdam,Tillsburg',
),
1 =>
array (
'country' => 'Sweden',
'city' => 'Stockholm',
),
)
答案 2 :(得分:0)
$headers = array();
$headers[] = array("country" => "Netherlands", "city" => "Amsterdam");
$headers[] = array("country" => "Netherlands", "city" => "Tillsburg");
$headers[] = array("country" => "Sweden", "city" => "Stockholm");
$result = array();
foreach($headers as $key => $value) {
$result[$value['country']][] = $value['city'];
}
$finalArray = array();
foreach($result as $k => $v) {
$finalArray[] = array('country' => $k, 'city' => join(',', $v));
}
答案 3 :(得分:-1)
使用国家/地区名称作为键创建另一个数组
<?php
$headers = array();
$headers[] = array("country" => "Netherlands", "city" => "Amsterdam");
$headers[] = array("country" => "Netherlands", "city" => "Tillsburg");
$headers[] = array("country" => "Sweden", "city" => "Stockholm");
$countries = array_unique(array_column($headers,"country"));
$newHeaders = array();
foreach($headers as $header){
$newHeaders[$header["country"]]["city"][] = $header["city"];
}
print_r($newHeaders);
foreach($newHeaders as $key=>$headers){
echo $key." City : ".implode(",",$headers['city'])."\n";
}
?>
现场演示Link
输出就像
Netherlands City : Amsterdam,Tillsburg
Sweden City : Stockholm