0

我必须将 2 个数组合并为一个数组,因为我已经尝试过 -

$TotalResult = array_merge_recursive($arr1, $arr2);

但是我没有通过这个功能得到想要的结果。

我的 2 个数组是:

$arr1 = Array(
[0] => Array
    (
        [search_terms] => facebook
        [search_count] => 1
    )

[1] => Array
    (
        [search_terms] => yahoomail.com
        [search_count] => 1
    )

[2] => Array
    (
        [search_terms] => google.com
        [search_count] => 1
    )

[3] => Array
    (
        [search_terms] => rediff.com
        [search_count] => 2
    )
);

$arr2=Array(
[0] => Array
    (
        [search_terms] => youtube
        [adclicks_count] => 1
    )

[1] => Array
    (
        [search_terms] => hotmail
        [adclicks_count] => 4
    )

[2] => Array
    (
        [search_terms] => facebook
        [adclicks_count] => 2
    )

[3] => Array
    (
        [search_terms] => google.com
        [adclicks_count] => 3
    )

[4] => Array
    (
        [search_terms] => yahoomail
        [adclicks_count] => 1
    )

[5] => Array
    (
        [search_terms] => stackoverflow
        [adclicks_count] => 1
    )
);

我希望得到的数组 3 如下 -

$arr3 = Array(
[0] => Array
    (
        [search_terms] => facebook
        [search_count] => 1
    [adclicks_count] => 2
    )

[1] => Array
    (
        [search_terms] => yahoomail.com
        [search_count] => 1
        [adclicks_count] => 1
    )

[2] => Array
    (
        [search_terms] => google.com
        [search_count] => 1
        [adclicks_count] => 3
    )

[3] => Array
    (
        [search_terms] => rediff.com
        [search_count] => 2
        [adclicks_count] => 0
    )

[4] => Array
    (
        [search_terms] => youtube
        [search_count] => 0
        [adclicks_count] => 1
    )

[5] => Array
    (
        [search_terms] => hotmail
        [search_count] => 0
        [adclicks_count] => 4
    )
[6] => Array
    (
        [search_terms] => stackoverflow
        [search_count] => 0
        [adclicks_count] => 1
    )
);

请建议我在 php.ini 中是否有任何可用的解决方案。

4

1 回答 1

0

一个简单的自定义脚本可以使技巧奏效

http://www.tehplayground.com/#7Uudu9PwY

$arr1 = array(
    array('search_terms' => 'facebook', 'search_count' => 1),
    array('search_terms' => 'whatever', 'search_count' => 4)
);


$arr2 = array(
    array('search_terms' => 'facebook', 'adclicks_count' => 2),
    array('search_terms' => 'whatever', 'adclicks_count' => 3)
);


$searchTermToData = array();

foreach ($arr1 as $element)
    $searchTermToData[$element['search_terms']] = $element;

foreach ($arr2 as $element)
    $searchTermToData[$element['search_terms']]['adclicks_count'] = $element['adclicks_count'];

// build result

$result = array_values($searchTermToData);
于 2013-04-13T07:31:54.093 回答