1

我有两个数组

Array
(
    [rows] => Array
        (
            [0] => Array
                (
                    [color] => 0
                )

            [1] => Array
                (
                    [color] => 1
                )

        )

)

Array
(
    [rows] => Array
        (
            [0] => Array
                (
                    [kaka] => 0
                )

            [1] => Array
                (
                    [kaka] => 1
                )

        )

)

我想合并那个数组,所以输出是

Array
(
    [rows] => Array
        (
            [0] => Array
                (
                    [color] => 0,
                    [kaka] => 0
                )

            [1] => Array
                (
                    [color] => 1,
                    [kaka] => 0
                )
        )
)

谁能帮我?

4

1 回答 1

0

尝试这个

$arr1 = array('row' => array(
                '0' => array ('color' => 0),
                '1' => array ('color' => 1)
              ));
$arr2 = array('row' => array(
                '0' => array ('kaka' => 0, 'dada' => 2),
                '1' => array ('kaka' => 1, 'tata' => 2),
                '2' => array ('kaka' => 2)
              ));

function merge($arr1, $arr2) {
    $result = $arr1;
    foreach($arr2['row'] as $key => $value) {
        foreach($value as $vkey => $vvalue) {
            $result['row'][$key][$vkey] = $vvalue;
        }
    }

    return $result;
}

$merge = merge($arr1, $arr2);
var_dump($merge);

输出

数组(大小=1)
  '行' =>
    数组(大小=3)
      0 =>
        数组(大小=3)
          '颜色' => int 0
          '卡卡' => int 0
          '爸爸' => int 2
      1 =>
        数组(大小=3)
          '颜色' => int 1
          '卡卡' => int 1
          'tata' => int 2
      2 =>
        数组(大小=1)
          '卡卡' => int 2
于 2013-08-27T03:36:40.017 回答