最简单的方法来执行此数组合并

时间:2013-05-20 12:59:16

标签: php

我有2个db查询从db返回值,这是最简单的方法。是否有可以执行此操作的PHP函数或是否必须使用循环?

$names = array(
        'det-1' => array('foo'),
        'det-2' => array('bar'),
        'det-3' => array('doe')
    );      

    $emails = array(
        'det-1' => array('foo@gmail.com'),
        'det-2' => array('bar@gmail.com'),
        'det-3' => array('doe@gmail.com')
    );

//结果

    $details = array(
        'det-1' => array('foo', 'foo@gmail.com')
        'det-2' => array('bar', 'bar@gmail.com')
        'det-3' => array('doe', 'doe@gmail.com')
    );

2 个答案:

答案 0 :(得分:2)

使用array_merge_recursive()

php > print_r(array_merge_recursive($names, $emails));
Array
(
    [det-1] => Array
        (
            [0] => foo
            [1] => foo@gmail.com
        )

    [det-2] => Array
        (
            [0] => bar
            [1] => bar@gmail.com
        )

    [det-3] => Array
        (
            [0] => doe
            [1] => doe@gmail.com
        )

)

答案 1 :(得分:0)

您可以使用array_merge_recursive

$newArray = array_merge_recursive($names, $emails);