合并两个数组合并为一个数组(同文键) [英] Merge two arrays into one array (identic keys)

查看:164
本文介绍了合并两个数组合并为一个数组(同文键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这是一个很简单的问题,但无法弄清楚如何这一点。我有这两个数组:

I think this is a very simple question, but can't figure out how to this. I've these two 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]
    )
)

我怎么能合并/合并他们,他们是这样的?

How can I combine/merge them that they look like this?

(
    [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]
    )
)

感谢您的帮助。

推荐答案

您可以尝试用这种方式:

You can try in this way:

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

)

更新:

通过这个功能您可以结合无限阵列(不仅二),即使它们有不同的大小:

Updated:

With this function you can combine infinite arrays (not only two), even if they have different sizes:

/*   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;
}

因此​​,用下面的code(三个数组):

So, with the following code (three arrays):

$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演示


  1. 现在,函数返回全部通过各行的值,不仅是第一个;

  2. 添加选项 T_OBJECT_CAST :传递常数<$​​ C $ C> T_OBJECT_CAST 数组列表之后,返回数组的行作为格式化为stdObjects而不是数组。

  1. Now the function return all passed values of each rows, not only the first;
  2. Added option T_OBJECT_CAST: passing constant T_OBJECT_CAST after the list of arrays, rows of returned array as formatted as stdObjects instead of arrays.

要允许未predetermined争论,我不格式化功能 multiArrayCombine($ ARG1,ARG2 $ ......),我用的不是< A HREF =htt​​p://php.net/manual/en/function.func-get-args.php相对=nofollow> func_get_args() 功能,即允许用户自定义函数接受可变长度参数列表。

Explanation:

To allow not predetermined arguments, I don't format function as multiArrayCombine( $arg1, $arg2, ... ), I use instead the func_get_args() function, that "allow user-defined functions to accept variable-length argument lists".

首先(在最新的更新),我检查,如果最后一个参数是predefined恒 T_OBJECT_CAST :如果是这样,我设置 $ asObject ,然后我的弹出它关闭的参数数组的结束;目前在的$ args 变量我有每个传递数组的数组。

First of all (in the latest update), I check if the last argument is the predefined constant T_OBJECT_CAST: if it is, I set $asObject to True, then I pop-it off the end of arguments array; now in the $args variable I have an array with each passed arrays.

下一步:我检索所有通过阵列的最大键值;我选择这样的方式,而不是更舒服的foreach($数组1为$行),以避免如果其他阵列中的一个具有多个第一行省略值。 最终没有数字键被省略了。

Next step: I retrieve the max key value of all passed arrays; i choose this way instead of more comfortable foreach( $array1 as $row ) to avoid to omit values if one of the other arrays have more rows than the first. Eventually not numeric keys are omitted.

然后,主回路:我处理原件阵列中的每一行,我添加其键和值排的意志加入到返回数组。 如果有重复键,只返回最后一个。

Then, the main loop: I process each row of originals arrays and I add their keys and values to row that will added to returned array. If there are duplicated keys, only the last is returned.

在处理每个数组后,我所获得的行(转换如果该选项被传递给对象)添加到阵列返回

After processing each array, i add the obtained row (converted to object if this option is passed) to returning array.

这就是全部!

全球语在这里,我很抱歉

这篇关于合并两个数组合并为一个数组(同文键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆