PHP - 将一个数组添加到另一个数组(递归?)

时间:2016-04-22 16:45:50

标签: php

我有两个数组:

$array1 = array(
  'currencies' => array(
    'dollars' => array(80,120,75),
    'euro' => array(25,35,10,85),
  ),
);

$array2 = array(
  'currencies' => array(
    'dollars' => array(25),
  ),
);

我希望结果是:

$result = array(
  'currencies' => array(
    'dollars' => array(80,120,75,25),
    'euro' => array(25,35,10,85),
  ),
);

这可以巧妙地完成吗?我试过了array_merge,array_merge_recursively等。

1 个答案:

答案 0 :(得分:5)

你是如何尝试array_merge_recursive的?因为这对我有用:

<?php
$array1 = array(
  'currencies' => array(
    'dollars' => array(80,120,75),
    'euro' => array(25,35,10,85),
  ),
);

$array2 = array(
  'currencies' => array(
    'dollars' => array(25),
  ),
);

var_dump(array_merge_recursive($array1, $array2));