简体   繁体   English

如何将数组的相似元素与 PHP 连接起来

[英]How to join similar elements of array with PHP

We are running a PHP code on a huge list (array with 5 millions elements).我们在一个巨大的列表(包含 500 万个元素的数组)上运行 PHP 代码。

The list format is as follows below (it looks "weird" at first look but this is the best format so far we came out in order to optimize the speed of the code down below)列表格式如下(第一眼看起来很“奇怪”,但这是迄今为止我们为了优化下面代码的速度而推出的最好的格式)

$array = array(

    array(1 => true,3 => true),
    array(2 => true,4 => true,6 => true),
    array(3 => true,5 => true),
    array(5 => true),
    array(4 => true,8 => true,10 => true),
    array(200 => true,300 => true)

);

We want to combine similar elements from the array above and get to this result:我们想组合上面数组中的相似元素并得到这个结果:

$final_array = array(

    array(1,3,5),
    array(2,4,6,8,10),
    array(200,300)

);

Instead of using array(1,3) we decided to use array(1 => true,3 => true) because using keys instead of values (to store information) makes the code below runs faster and it outputs $final_array as above.我们决定使用array(1 => true,3 => true)而不是使用array(1,3) ,因为使用键而不是值(存储信息)使下面的代码运行得更快,并且它像上面一样输出$final_array

foreach ($array as $key1 => $value1) {

    foreach ($array as $key2 => $value2) {

        if ($key1 != $key2) {

            foreach ($array[$key1] as $key3 => $value3) {

                if (isset($array[$key2][$key3])) {

                    $array[$key2] = $array[$key2] + $array[$key1];

                    unset($array[$key1]);

                    break 2;

                }

            }   

        }

    }

}

However this code above is still very slow.但是,上面的代码仍然很慢。 Could you find a better way to aggregate similar elements to each other on a faster code?你能找到一种更好的方法在更快的代码上聚合相似的元素吗?

How about using array_intersect_key instead of third loop?如何使用array_intersect_key而不是第三个循环?

$array = array(
    array(1 => true,3 => true),
    array(2 => true,4 => true,6 => true),
    array(3 => true,5 => true),
    array(5 => true),
    array(4 => true,8 => true,10 => true),
    array(200 => true,300 => true)

);
foreach ($array as $key => $value) {
    foreach ($array as $key2 => $value2) {
        if ($key !== $key2 && !empty(array_intersect_key($value, $value2))) {
            $array[$key] = $value2 + $value;
            unset($array[$key2]);
        }
    }
}

print_r($array);

Working example 工作示例

Edit #1: For better performance try this variant:编辑#1:为了获得更好的性能,试试这个变体:

$array = array(
    array(1 => true,3 => true),
    array(2 => true,4 => true,6 => true),
    array(3 => true,5 => true),
    array(5 => true),
    array(4 => true,8 => true,10 => true),
    array(200 => true,300 => true)

);
$count = count($array);
for ($i = 0; $i < $count - 1; ++$i) {
    for ($j = $i + 1; $j < $count; ++$j) {
        if (!empty(array_intersect_key($array[$i], $array[$j]))) {
            $array[$j] = $array[$i] + $array[$j];
            unset($array[$i]);
            continue 2;
        }
    }
}

Working example #2 工作示例#2

$arr = array('Hello','World!','Beautiful','Day!');
echo join(" ",$arr);

See Details 阅读详情

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

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