简体   繁体   English

将数组中的相关元素分组

[英]Group related elements in array

I have an array in the following format: 我有以下格式的数组:

[8106] => Array (
    [id1] => 210470
    [id2] => 216298
)

[8107] => Array (
    [id1] => 210470
    [id2] => 187145
)

[8108] => Array (
    [id1] => 187145
    [id2] => 216298
)

[8109] => Array (
    [id1] => 187145
    [id2] => 210470
)

[8110] => Array (
    [id1] => 266533
    [id2] => 249612
)
[8111] => Array (
    [id1] => 249612
    [id2] => 266533
)

I need to get it into the following format: 我需要将其转换为以下格式:

[0] => Array (
    [0] => 266533
    [1] => 249612
)
[1] => Array (
    [0] => 187145
    [1] => 210470
    [2] => 216298
)

Basically, I need to extract all the ids, keep the relationships, but group them all together. 基本上,我需要提取所有id,保留关系,然后将它们全部组合在一起。 I have a function to do this, but it takes forever (I am up to 30+ minutes on the number of rows I have to run through). 我有一个函数可以执行此操作,但是它要花很多时间(我必须经过30多分钟才能完成的行数)。 Keys and order are unimportant. 键和顺序并不重要。 The relationship is all that is important. 关系很重要。 I am looking for a faster method. 我正在寻找一种更快的方法。 The function(s) I am using are below: 我正在使用的功能如下:

function getMatchingIDs($filteredArray)
{
    $result = array();

    $resultCount = 0;
    foreach ($filteredArray as $details) {
        $imaId1 = inMultiArray($details['id1'], $result);
        $imaId2 = inMultiArray($details['id2'], $result);

        if ($imaId1 === false && $imaId2 === false) {
            $result[$resultCount++] = array(
                $details['id1'],
                $details['id2'],
            );
        } elseif (is_numeric($imaId1) === true && $imaId2 === false) {
            $result[$imaId1][] = $details['id2'];
        } elseif ($imaId1 === false && is_numeric($imaId2) === true) {
            $result[$imaId2][] = $details['id1'];
        } elseif ($imaId2 != $imaId1) {
            $result[$imaId1] = array_merge($result[$imaId1], $result[$imaId2]);
            unset($result[$imaId2]);
        }
    }

    return $result;
}

function inMultiArray($elem, $array)
{
    if (is_array($array) === true) {
        // if the variable $elem is in the variable $array return true
        if (is_array($array) === true && in_array($elem, $array) === true) {
            return true;
        }

        // if $elem isn't in $array, then check foreach element
        foreach ($array as $key => $arrayElement) {
            // if $arrayElement is an array call the inMultiArray function to this element
            // if inMultiArray returns true, than return is in array, else check next element
            if (is_array($arrayElement) === true) {
                $value = inMultiArray($elem, $arrayElement);
                if ($value === true) {
                    return $key;
                }
            }
        }
    }

    // if isn't in array return false
    return false;
}

$filtered = getMatchingIDs($unfiltered);

EDIT: The original array describes relations between pairs of ids (not shown in the array). 编辑:原始数组描述了ID对之间的关​​系(数组中未显示)。 The desired output is that the relations are further defined. 期望的输出是进一步定义关系。 If you look in the original array, elements 8106-8109 are simply paired combinations of three ids. 如果您查看原始数组,则元素8106-8109只是三个ID的成对组合。 I need those three grouped together. 我需要将这三个分组在一起。 Elements 8110 and 8111 are a distinct pair, just in a different order. 元素8110和8111是一对不同的对,只是顺序不同。

$newArray = array();
foreach ($array as $k => $v) {
  $newArray[0][] = $v['id1'];
  $newArray[1][] = $v['id2'];
}

What I finally ended up doing was in essence creating an index array. 我最终最终要做的实际上是创建一个索引数组。 This array held all the positions of each value in the primary array. 该数组保存主数组中每个值的所有位置。

So the following array 所以下面的数组

[0] => Array (
    [0] => 266533
    [1] => 249612
)
[1] => Array (
    [0] => 187145
    [1] => 210470
    [2] => 216298
)

has an index of: 索引为:

[187145] => 1
[210470] => 1
[216298] => 1
[249612] => 0
[266533] => 0

So instead of looking for the value in the primary multidimensional array, I check to see if it exists in the index array and process the data based on that. 因此,我没有在主要的多维数组中查找值,而是检查它是否存在于索引数组中并根据该值处理数据。 The results are that it now runs the entire process in <5 seconds instead of > 1 hour. 结果是它现在可以在<5秒而不是> 1小时内运行整个过程。

Thank you for your help. 谢谢您的帮助。

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

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