简体   繁体   English

合并具有相同值的数组

[英]Combine arrays that have same value

I have array like this 我有这样的数组

$arr=[["a","b"],["b","c"],["d","e"],["f","c"]];

if sub arrays share same value they should be be merged to one array 如果子数组共享相同的值,则应将它们合并到一个数组中

expected output: 预期输出:

$arr=[["a","b","c","f"],["d","e"]];

I`m trying to avoid doing foreach inside foreach for solving this. 我试图避免在foreach内部执行foreach来解决此问题。

It seems your inner arrays always have 2 items. 看来您的内部数组总是有2个项目。 so nested loops aren't necessary. 因此不需要嵌套循环。 Here is a solution which I originally wrote in JS but it should work just as good and most efficient in PHP: 这是我最初用JS编写的一种解决方案,但它在PHP中应同样有效最有效

$arr=[["a","b"],["b","c"],["d","e"],["f","c"],["h","e"]];
$output = [];
$outputKeys = [];
$counter = 0;
foreach($arr as $V) {
    if(!isset($outputKeys[$V[0]]) && !isset($outputKeys[$V[1]])) {
        $output[$counter] = [$V[0], $V[1]];
        $outputKeys[$V[0]] = &$output[$counter];
        $outputKeys[$V[1]] = &$output[$counter];
        $counter++;
    }
    elseif(isset($outputKeys[$V[0]]) && !isset($outputKeys[$V[1]])) {
        array_push($outputKeys[$V[0]], $V[1]);
        $outputKeys[$V[1]] = &$outputKeys[$V[0]];
    }
    elseif(!isset($outputKeys[$V[0]]) && isset($outputKeys[$V[1]])) {
        array_push($outputKeys[$V[1]], $V[0]);
        $outputKeys[$V[0]] = &$outputKeys[$V[1]];
    }
}
var_dump($output); // [["a","b","c","f"],["d","e","h"]]

DEMO (click the execute button) 演示(单击执行按钮)

Pointers are your friends. 指针是您的朋友。 Use them :) 使用它们 :)

This is solution I get for now. 这是我现在得到的解决方案。

    $arr=[["a","b","c","f"],["d","e"]];
    $sortedArray = sortFunction($arr,0,array());

function sortFunction($old,$index,$new) {
    if ($index == sizeof($old)) return $new;

    for ($i = 0; $i<sizeof($new); $i++) {
        if (count(array_intersect($new[$i],$old[$index]))) {
            $new[$i] = array_unique(array_merge($old[$index],$new[$i]), SORT_REGULAR);
            return sortFunction($old,$index + 1,$new);
        }
    }

    $new[] = $old[$index];
    return sortFunction($old,$index + 1,$new);
}

The following algorithm should do what you want. 下面的算法应该做您想要的。 It simply checks through each item and checks if it already exists in the newly created array, and if it does it adds it to that item instead of a new one: 它只是简单地检查每个项目,并检查它是否已经存在于新创建的数组中,如果确实存在,它将添加到该项目中,而不是将其添加到新项目中:

<?php

$arr=[["a","b"],["b","c"],["d","e"],["f","c"]];

$newArr = [];

foreach ($arr as $items) {
    $newKey = null;

    foreach ($items as $item) {
        foreach ($newArr as $newItemsKey => $newItems) {
            if (in_array($item, $newItems)) {
                $newKey = $newItemsKey;

                break 2;
            }
        }
    }

    if ($newKey !== null) {
        $newArr[$newKey] = array_merge($newArr[$newKey], $items);
    } else {
        $newArr[] = $items;
    }
}

$newArr = array_map('array_unique', $newArr);

print_r($newArr);

Output : 输出

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [3] => c
            [4] => f
        )

    [1] => Array
        (
            [0] => d
            [1] => e
        )

)

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM