1

我有两个这样的数组

array (
[0] => Array
    (
        [search_terms] => Big Black Gay
        [searches] => 1
    )

[1] => Array
    (
        [search_terms] => Men Seeking Men Personals
        [searches] => 1
    )
)

另一个这样的数组

   array (
   [0] => Array
    (
        [search_terms] => Men Seeking Men Personals
        [clicks] => 1
    )
   )

如何在不使用任何循环的情况下合并两个数组?任何 php 功能都可用于此?我需要这种输出

Array
(
[0] => Array
    (
        [search_terms] => Big Black Gay
        [searches] => 1
        [clicks] => 0
    )

[1] => Array
    (
        [search_terms] => Men Seeking Men Personals
        [searches] => 1
        [clicks] => 1
    )

)
4

2 回答 2

5

看看array_merge

var_dump(array_merge($array_one,$array_two));
于 2013-03-20T08:39:53.943 回答
2

你可以试试

$data = array(
        0 => array(
                'search_terms' => 'Big Black Gay',
                'searches' => 1
        ),
        1 => array(
                'search_terms' => 'Men Seeking Men Personals',
                'searches' => 1
        )
);

$combine = array(
        0 => Array(
                'search_terms' => 'Men Seeking Men Personals',
                'clicks' => 1
        )
);

foreach ( $data as &$item ) {
    foreach ( $combine as $new ) {
        if ($item['search_terms'] == $new['search_terms'])
            $item['clicks'] = $new['clicks'];
    }

    if (! isset($item['clicks']))
        $item['clicks'] = 0;
}

print_r($data);

输出

Array
(
    [0] => Array
        (
            [search_terms] => Big Black Gay
            [searches] => 1
            [clicks] => 0
        )

    [1] => Array
        (
            [search_terms] => Men Seeking Men Personals
            [searches] => 1
            [clicks] => 1
        )

)
于 2013-03-20T08:45:09.527 回答