1

このような2つの配列があります

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
    )
   )

ループを使用せずに上記の 2 つの配列をマージするにはどうすればよいですか? これに使用できる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 に答える