我必须将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中是否有任何解决方案。
答案 0 :(得分: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);