如何在php中的第一个数组中合并第二个数组

时间:2017-02-14 10:59:01

标签: php arrays

我需要什么

  • 我需要将两个数组与第二个数组合并到同一个索引中的第一个数组中。

数组输出

Array
(
[0] => Array
    (
        [RESPONSE] => 2
        [RESPONSE_TEXT] => 
        [DEVELOPER_TEXT] => 
        [RESPONSE_DATE_TIME] => 14-FEB-2017 11:09
        [RESPONSE_DATE] => 2017-02-14 11:09:52
        [RESPONSE_BY_ID] => 84
        [RESPONSE_ASSIGNED_USER_NAME] => bb
    )

[1] => Array
    (
        [RESPONSE] => 1
        [RESPONSE_TEXT] => 
        [DEVELOPER_TEXT] => 
        [RESPONSE_DATE_TIME] => 16-JAN-2017 14:07
        [RESPONSE_DATE] => 2017-01-16 14:07:47
        [RESPONSE_BY_ID] => 1
        [RESPONSE_ASSIGNED_USER_NAME] => aaa
    )

[2] => Array
    (
        [RESPONSE] => 3
        [RESPONSE_TEXT] => 
        [DEVELOPER_TEXT] => 
        [RESPONSE_DATE_TIME] => 16-JAN-2017 14:02
        [RESPONSE_DATE] => 2017-01-16 14:02:01
        [RESPONSE_BY_ID] => 1
        [RESPONSE_ASSIGNED_USER_NAME] => cc
    )

)

Array
(
    [0] => Array
    (
        [RESPONSE_USER_NAME] => my name
    )

  )

php code

   print_r(array_merge(array_values($result1), array_values($result2)));

上面的PHP代码

    Array
   (

   [0] => Array
   (
    [RESPONSE] => 2
    [RESPONSE_TEXT] => 
    [DEVELOPER_TEXT] => 
    [RESPONSE_DATE_TIME] => 14-FEB-2017 11:09
    [RESPONSE_DATE] => 2017-02-14 11:09:52
    [RESPONSE_BY_ID] => 84
    [RESPONSE_ASSIGNED_USER_NAME] => bb
   )
   [1] => Array
    (
        [RESPONSE_USER_NAME] => my name
    )

   )
  • 我需要输出

       Array
         (
            [0] => Array
          (
        [RESPONSE] => 2
        [RESPONSE_TEXT] => 
        [DEVELOPER_TEXT] => 
        [RESPONSE_DATE_TIME] => 14-FEB-2017 11:09
        [RESPONSE_DATE] => 2017-02-14 11:09:52
        [RESPONSE_BY_ID] => 84
        [RESPONSE_ASSIGNED_USER_NAME] => bb
        [RESPONSE_USER_NAME] => my name
          )
    
        [1] => Array
       (
        [RESPONSE] => 1
        [RESPONSE_TEXT] => 
        [DEVELOPER_TEXT] => 
        [RESPONSE_DATE_TIME] => 16-JAN-2017 14:07
        [RESPONSE_DATE] => 2017-01-16 14:07:47
        [RESPONSE_BY_ID] => 1
        [RESPONSE_ASSIGNED_USER_NAME] => aa
        [RESPONSE_USER_NAME] => my name
        )
    

问题

  • 我已经尝试过array_merge php函数,但它在第一个数组中附加了另一个数组索引。

  • 请为此问题提出解决方案

3 个答案:

答案 0 :(得分:0)

看起来你不想合并。你想要的是将数组中的每个数组与另一个数组中的数组合并。 做一些像:

foreach ($result1 as $array){
   array_merge($array, $result[0])
}

答案 1 :(得分:0)

试试这个

foreach ($result1 as $key=>$array){
   array_merge($array, $result[$key])
}

答案 2 :(得分:0)

正在寻找类似的东西吗?

Array
(
    [0] => Array
        (
            [RESPONSE] => 2
            [RESPONSE_TEXT] => 
            [DEVELOPER_TEXT] => 
            [RESPONSE_DATE_TIME] => 14-FEB-2017 11:09
            [RESPONSE_DATE] => 2017-02-14 11:09:52
            [RESPONSE_BY_ID] => 84
            [RESPONSE_ASSIGNED_USER_NAME] => bb
            [RESPONSE_USER_NAME] => my name
        )

    [1] => Array
        (
            [RESPONSE] => 2
            [RESPONSE_TEXT] => 
            [DEVELOPER_TEXT] => 
            [RESPONSE_DATE_TIME] => 14-FEB-2017 11:09
            [RESPONSE_DATE] => 2017-02-14 11:09:52
            [RESPONSE_BY_ID] => 5
            [RESPONSE_ASSIGNED_USER_NAME] => cc
            [RESPONSE_USER_NAME] => my name
        )

    [2] => Array
        (
            [RESPONSE] => 2
            [RESPONSE_TEXT] => 
            [DEVELOPER_TEXT] => 
            [RESPONSE_DATE_TIME] => 14-FEB-2017 11:09
            [RESPONSE_DATE] => 2017-02-14 11:09:52
            [RESPONSE_BY_ID] => 6
            [RESPONSE_ASSIGNED_USER_NAME] => ee
            [RESPONSE_USER_NAME] => my name
        )

)

输出:

{{1}}