php 组合数组中合并元素[重复]

9jyewag0  于 2023-03-28  发布在  PHP
关注(0)|答案(3)|浏览(185)

此问题在此处已有答案

Can't concatenate 2 arrays in PHP(11个答案)
昨天关门了。
这篇文章是6小时前编辑并提交审查的。
我不知道为什么这个问题被标记为重复,因为所谓的重复是关于使用array_merge合并数组的-这个问题是关于组合数组中的元素树。
有没有一种方法可以在PHP数组中合并单个元素及其子树?例如,转换如下:

$test = array(
    "EW" => array(313, 1788),
    "SC" => array(670, 860),
    "FR" => array(704, 709),
    "UK" => array(423, 1733)
);

变成这样:

$test1 = array(
    "UK" => array(313, 423, 670, 860, 1733, 1788),
    "FR" => array(704, 709)
);

其中EW和SC的条目已与UK的条目合并。
array_combinearray_merge似乎在数组之间工作,而不是在数组内部工作,所以我不清楚如何实现这一点。

hof1towb

hof1towb1#

你必须为那些必须与另一个键(下面代码中的$group_keys)分组的键创建一个Map。

$test= [
    'EW' => [313, 1788], 
    'SC' => [670, 860], 
    'FR' => [704, 709], 
    'UK' => [423, 1733]
];

$group_keys=[
    'EW' => 'UK', 
    'SC' => 'UK'
];

$result = [];
foreach($test as $code => $values) {
    if (isset($group_keys[$code])) {
        $target = $group_keys[$code];
    }
    else {
        $target = $code;
    }
    if (isset($result[$target])) {
        $result[$target] = array_merge($result[$target], $values);
    }
    else {
        $result[$target] = $values;
    }
}

print_r($result);

输出:

Array
(
    [UK] => Array
        (
            [0] => 313
            [1] => 1788
            [2] => 670
            [3] => 860
            [4] => 423
            [5] => 1733
        )

    [FR] => Array
        (
            [0] => 704
            [1] => 709
        )

)
wj8zmpe1

wj8zmpe12#

一种方法是这样的:

$test=array("EW"=>array(313, 1788), "SC"=>array(670, 860), "FR"=>array(704, 709), "UK"=>array(423, 1733));

//extract function converts all the keys to variables. 

extract($test);
$newarray['UK'] = array_merge($EW,$SC,$UK) ;
$newarray['FR'] = $FR;


echo "<pre>";
print_r($newarray);
echo "</pre>";

结果是:

Array
(
    [UK] => Array
        (
            [0] => 313
            [1] => 1788
            [2] => 670
            [3] => 860
            [4] => 423
            [5] => 1733
        )

    [FR] => Array
        (
            [0] => 704
            [1] => 709
        )

)

对于小的用例,你知道的关键,它不会改变这个方法是可以的...

798qvoo8

798qvoo83#

函数{array_merge_trees}将接受一个数组,剪切出要合并的树并合并它们,从原始数组中删除它们,并将合并后的树连接到原始数组的剩余部分:

function array_merge_trees($original)
// Merge individual trees within an array.  
{
    $mergeme=array(UK, EW, SC, NI, DE, ES, FR, IT);  //The trees you want to merge.  IMPORTANT: The trees must exist within the original array, or you will get empty trees in the joined array.
    $trim=array_flip($mergeme);  // Convert the $mergeme values to keys.
    extract(array_intersect_key($original, $trim));  // Extract the trees in $mergeme - they will each become an array named after the key, eg $UK, $EW.
    $merged['UK']=array_merge($UK, $EW, $SC, $NI);  // Merge extracted trees as an element of a new array. IMPORTANT: The trees listed here must exist within the original array, or you will get empty trees in the joined array.
    sort($merged['UK']);  // Sort the values of the merged tree.
    // Repeat the above two lines if you want further merges, eg:
    $merged['EU'] = array_merge($DE, $ES, $FR, $IT);
    sort($merged['EU']);
    $trimmed=array_diff_key($original, $trim);  // Remove the trees to be merged from the original array.
    $rejoined=array_merge($trimmed, $merged);  // Join the merged trees with the non-merged trees from the original array.
    ksort($rejoined);  // Sort the keys of the rejoined array.
    return $rejoined;
}

使用方法:

$original=array("EW"=>array(313, 1788), "SC"=>array(670, 860), "FR"=>array(704, 709), "UK"=>array(423, 1733), "DE"=>array(220, 260), "ES"=>array(1346, 1229), "NI"=>array(410, 453), "IT"=>array(134, 988));

$original=array_merge_trees($original);

echo "<pre>";
print_r($original);
echo "</pre>";

编辑:洪克的回答比这个好得多。

相关问题