高性能数组连接?

时间:2018-01-30 20:23:41

标签: php arrays

我想对关联数组数组应用与SQL相同的概念。以下是用户及其地址的示例,以及名为array_join(array $array1, array $array2, string $key1, string $key2, string $new_key)

的函数的所需输出下方

用户

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => John
        )

    [1] => Array
        (
            [id] => 2
            [name] => Foo
        )

    [2] => Array
        (
            [id] => 3
            [name] => Baz
        )

)

地址

Array
(
    [0] => Array
        (
            [id] => 1
            [user_id] => 1
            [street] => 53 Van Ness St
        )

    [1] => Array
        (
            [id] => 2
            [user_id] => 1
            [street] => 21 Overland St
        )

    [2] => Array
        (
            [id] => 3
            [user_id] => 1
            [street] => 101 Mountfort St
        )

    [3] => Array
        (
            [id] => 4
            [user_id] => 3
            [street] => 27 Sidney St
        )

)

(总结一下,用户1有3个地址,用户3有一个)

所需的输出

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => John
            [addresses] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [user_id] => 1
                            [street] => 53 Van Ness St
                        )

                    [1] => Array
                        (
                            [id] => 2
                            [user_id] => 1
                            [street] => 21 Overland St
                        )

                    [2] => Array
                        (
                            [id] => 3
                            [user_id] => 1
                            [street] => 101 Mountfort St
                        )

                )

        )

    [1] => Array
        (
            [id] => 2
            [name] => Foo
        )

    [2] => Array
        (
            [id] => 3
            [name] => Baz
            [addresses] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [user_id] => 3
                            [street] => 27 Sidney St
                        )

                )

        )

)

现在我想出的是:

<?php

function array_join(&$array1, $array2, $key1, $key2, $new_key) {
    foreach( $array1 as $index1 => $row1 ) {
        foreach( $array2 as $index2 => $row2 ) {
            if( $row2[$key2] === $row1[$key1] ) {
                $array1[$index1][$new_key][] = $row2;
            }
        }
    }
}

如何让这个功能更快地计算数据,知道平均需要1.2秒,这对于6000行非常高......我该如何优化呢?

1 个答案:

答案 0 :(得分:2)

不要使用嵌套循环。创建一个使用$key1作为键的关联数组。然后你可以循环遍历$array2并找到$array1的匹配元素,而不需要循环。

function array_join(&$array1, $array2, $key1, $key2, $new_key) {
    $assoc_array1 = array();
    // Create an associative array of references to the original array elements, keyed by $key1
    foreach ($array1 as &$row1) {
        $assoc_array1[$row1[$key1]] = $row1;
    }
    // Join the elements of the second array to those elements.
    foreach( $array2 as $row2 ) {
        $assoc_array1[$row2[$key2]][$new_key][] = $row2;
    }
}

这类似于数据库使用索引来优化JOIN操作的方式。