将两个数组合并为一个数组(identic键)

时间:2016-01-27 15:23:32

标签: php arrays

我认为这是一个非常简单的问题,但无法弄清楚如何做到这一点。我有这两个数组:

(
    [0] => Array
    (
        [accordion_title] => [accordion-title]Title 1[/accordion-title]
    )
    [1] => Array
    (
        [accordion_title] => [accordion-title]Title 2[/accordion-title]
    )
)

(
    [0] => Array
    (
        [accordion_content] => [accordion-content]Content 1[/accordion-content]
    )
    [1] => Array
    (
        [accordion_content] => [accordion-content]Content 2[/accordion-content]
    )
)

如何组合/合并它们看起来像这样?

(
    [0] => Array
    (
        [accordion_title] => [accordion-title]Title 1[/accordion-title]
        [accordion_content] => [accordion-content]Content 1[/accordion-content]
    )
    [1] => Array
    (
        [accordion_title] => [accordion-title]Title 2[/accordion-title]
        [accordion_content] => [accordion-content]Content 2[/accordion-content]
    )
)

感谢您的帮助。

4 个答案:

答案 0 :(得分:2)

您可以尝试这种方式:

$array1 = array( array('accordion_title'=>'Title 1'),array('accordion_title'=>'Title 2') );
$array2 = array( array('accordion_content'=>'Content 1'),array('accordion_content'=>'Content 2') );

$array3 = array();
foreach( $array1 as $key => $array )
{
    $array3[] = array( key($array) => current($array), key($array2[$key]) => current($array2[$key]) );
}

print_r( $array3 );

这是输出:

Array
(
    [0] => Array
        (
            [accordion_title] => Title 1
            [accordion_content] => Content 1
        )

    [1] => Array
        (
            [accordion_title] => Title 2
            [accordion_content] => Content 2
        )

)

更新

使用此功能,您可以组合无限阵列(不仅仅是两个),即使它们具有不同的大小:

/*   Groups passed arrays in an array of associative arrays with same keys and values
 *
 *   @example          $array1 = array( array('a'=>'val1'),array('a'=>'val2') );
 *                     $array2 = array( array('b'=>'val3'),array('b'=>'val4') );
 *                     $array3 = array( array('c'=>'val5'),array(),array('c'=>'val6') );
 *                     multiArrayCombine( $array1, $array2, $array3 );
 *                     return: array
 *                     (
 *                        0 => array('a'=>'val1','b'=>'val3','c'=>'val5'),
 *                        1 => array('a'=>'val2','b'=>'val4'),
 *                        2 => array('c'=>'val6')
 *                     )
 *                     
 *   @param   array    $array1[, $array2[, $array3...]]
 *
 *   @option  const    T_OBJECT_CAST cast returned assoc arrays as stdObject
 *
 *   @return  array
 */
function multiArrayCombine()
{
    /* Get all passed parameters and T_OBJECT_CAST option: */
    $args     = func_get_args();
    $asObject = ( T_OBJECT_CAST == end($args) );
    if( $asObject ) array_pop( $args );

    $retval = array();          # Init array to be returned

    /* Retrieve highest passed arrays key: */
    $max = 0;
    foreach( $args as $array ) $max = max( $max, max( array_keys($array) ) );

    /* Loop for each arrays key: */
    for( $i=0; $i<=$max; $i++ )
    {
        /* Init associative array to add:  */
        $add = array();

        /* Process actual key ($i) of each passed array:  */
        foreach( $args as $array )
        {
            /* If the key ($i) exists, add  each passed array:  */
            if( isset($array[$i]) AND is_array($array[$i]) )
            {
                foreach( $array[$i] as $key => $val )
                { $add[$key] = $val; }
            }
        }

        /* Add the obtained associative array to return array */
        if( $asObject ) $retval[] = (object) $add;
        else            $retval[] = $add;
    }

    return $retval;
}

所以,使用以下代码(三个数组):

$array1 = array( array('accordion_title'=>'Title 1'),array('accordion_title'=>'Title 2') );
$array2 = array( array('accordion_content'=>'Content 1'),array('accordion_content'=>'Content 2') );
$array3 = array( array('accordion_date'=>'Date 1'),array(),array('accordion_date'=>'Date 3') );

print_r( multiArrayCombine( $array1, $array2, $array3 ) );

输出是:

Array
(
    [0] => Array
        (
            [accordion_title] => Title 1
            [accordion_content] => Content 1
            [accordion_date] => Date 1
        )

    [1] => Array
        (
            [accordion_title] => Title 2
            [accordion_content] => Content 2
        )

    [2] => Array
        (
            [accordion_date] => Date 3
        )
)

eval.in demo

更新2:

  1. 现在函数返回所有传递的每行值,而不仅仅是第一行;
  2. 添加了选项T_OBJECT_CAST:在数组列表后传递常量T_OBJECT_CAST,返回数组的行格式为stdObjects而不是数组。
  3. 说明:

    为了不允许预定的参数,我不将函数格式化为multiArrayCombine( $arg1, $arg2, ... ),而是使用func_get_args()函数,“允许用户定义的函数接受可变长度的参数列表”。 / p>

    首先(在最新的更新中),我检查最后一个参数是否是预定义常量T_OBJECT_CAST:如果是,我将$asObject设置为True,然后我{ {3}}在arguments数组的末尾;现在在$args变量中,每个传递的数组都有一个数组。

    下一步:我检索所有传递的数组的最大键值;我选择这种方式而不是更舒适foreach( $array1 as $row ),以避免在其他数组之一的行数多于第一个数据时省略值。 最终不会删除数字键。

    然后,主循环:我处理每一行原始数组,并将它们的键和值添加到将添加到返回数组的行。 如果有重复的密钥,则仅返回最后一个密钥。

    在处理完每个数组之后,我将获得的行(如果传递了这个选项,则转换为object)添加到返回的数组中。

    这就是全部!

    在这里结束,对不起

答案 1 :(得分:1)

这样的事情应该有效:

$merged = [];
$array1 = [ ... ];
$array2 = [ ... ];
foreach ($array1 as $key => $value) {
    $merged[$key][] = $value;
}
foreach ($array2 as $key => $value) {
    $merged[$key][] = $value;
}

我希望我在这里所做的事情是有道理的。

答案 2 :(得分:0)

只需通过 array_merge() 进行迭代 array_map() 调用,然后传入所有应合并的数组。

代码:(Demo)

$array1 = [
    ['accordion_title' => '[accordion-title]Title 1[/accordion-title]'],
    ['accordion_title' => '[accordion-title]Title 2[/accordion-title]']
];
$array2 = [
    ['accordion_content' => '[accordion-content]Content 1[/accordion-content]'],
    ['accordion_content' => '[accordion-content]Content 2[/accordion-content]']
];

var_export(array_map('array_merge', $array1, $array2));

输出:

array (
  0 => 
  array (
    'accordion_title' => '[accordion-title]Title 1[/accordion-title]',
    'accordion_content' => '[accordion-content]Content 1[/accordion-content]',
  ),
  1 => 
  array (
    'accordion_title' => '[accordion-title]Title 2[/accordion-title]',
    'accordion_content' => '[accordion-content]Content 2[/accordion-content]',
  ),
)

答案 3 :(得分:-1)

也许使用php函数array array_merge($array1 ,$array2)

http://php.net/manual/fr/function.array-merge.php