我有3个独立的数组,它们存储在一个大数组中,如下所示:
$date = '2017-08-01';
$price_arr_1 = array();
$price_arr_1[$date]['adult_1'] = 10;
$price_arr_1[$date]['child_1'] = 2;
$price_arr_2 = array();
$price_arr_2[$date]['adult_2'] = 10;
$price_arr_2[$date]['child_2'] = 2;
$price_arr_3 = array();
$price_arr_3[$date]['adult_3'] = 10;
$price_arr_3[$date]['child_3'] = 2;
$multiple_arrays[] = $price_arr_1;
$multiple_arrays[] = $price_arr_2;
$multiple_arrays[] = $price_arr_3;
print_r($multiple_arrays);
输出结果为:
Array ( [0] => Array ( [2017-08-01] => Array ( [adult_1] => 10 [child_1] => 2 ) ) [1] => Array ( [2017-08-01] => Array ( [adult_2] => 10 [child_2] => 2 ) ) [2] => Array ( [2017-08-01] => Array ( [adult_3] => 10 [child_3] => 2 ) ) )
我想使用array_merge_recursive()将三个数组合并为一个这样的数组:
Array ( [2017-08-01] => Array ( [adult_1] => 10 [child_1] => 2 [adult_2] => 10 [child_2] => 2 [adult_3] => 10 [child_3] => 2 ) )
我认为在主阵列中循环可能会有效,但我无法理解它,感谢任何帮助!
我无法执行以下操作,因为每次数组中的数组数量都不同:
print_r(array_merge_recursive($multiple_arrays[0],$multiple_arrays[1],$multiple_arrays[2]));
答案 0 :(得分:2)
要传递给array_merge_recursive
多个参数,您可以使用call_user_func_array
。在你的情况下,它看起来像:
$date = '2017-08-01';
$price_arr_1 = array();
$price_arr_1[$date]['adult_1'] = 10;
$price_arr_1[$date]['child_1'] = 2;
$price_arr_2 = array();
$price_arr_2[$date]['adult_2'] = 10;
$price_arr_2[$date]['child_2'] = 2;
$price_arr_3 = array();
$price_arr_3[$date]['adult_3'] = 10;
$price_arr_3[$date]['child_3'] = 2;
$multiple_arrays[] = $price_arr_1;
$multiple_arrays[] = $price_arr_2;
$multiple_arrays[] = $price_arr_3;
//print_r($multiple_arrays);
$r = call_user_func_array('array_merge_recursive', $multiple_arrays);
print_r($r);