我有两个数组..数组1和数组2。 两个数组都包含 2 条记录。
现在我需要将两个数组合并到一个数组中。
在我的第二个数组中,我从 u_data 中得到了以下两个字段。
[totalcharge] => 15
[RoadTotal] => 15
现在我需要将这些字段合并到第一个数组中。
我需要合并多个数组。下面是问题:
我尝试过这样的事情......但我无法达到我的预期输出。
$abc = array_merge($content, $modecost);
数组1
Array
(
[0] => Array
(
[u_data] => Array
(
[Mode] => Rail
[TotalCost] => 150
)
[0] => Array
(
[CostSavingRoutes] => 1
[RailCostSaving] => 150
[Costsavingshipments] => 2
)
[origin_city] => Array
(
[origin_pcode] => 2046
)
[dest_city] => Array
(
[dest_pcode] => 4361
)
)
[1] => Array
(
[u_data] => Array
(
[Mode] => Rail
[TotalCost] => 150
)
[0] => Array
(
[CostSavingRoutes] => 1
[RailCostSaving] => 150
[Costsavingshipments] => 2
)
[origin_city] => Array
(
[origin_pcode] => 3170
)
[dest_city] => Array
(
[dest_pcode] => 4670
)
)
)
数组2
Array
(
[0] => Array
(
[u_data] => Array
(
[totalcharge] => 15
[RoadTotal] => 15
)
)
[1] => Array
(
[u_data] => Array
(
[totalcharge] => 15
[RoadTotal] => 15
)
)
)
最终我希望它看起来像这样
Array
(
[0] => Array
(
[u_data] => Array
(
[Mode] => Rail
[TotalCost] => 150
[totalcharge] => 15
[RoadTotal] => 15
)
[0] => Array
(
[CostSavingRoutes] => 1
[RailCostSaving] => 150
[Costsavingshipments] => 2
)
[origin_city] => Array
(
[origin_pcode] => 2046
)
[dest_city] => Array
(
[dest_pcode] => 4361
)
)
[1] => Array
(
[u_data] => Array
(
[Mode] => Rail
[TotalCost] => 150
[totalcharge] => 15
[RoadTotal] => 15
)
[0] => Array
(
[CostSavingRoutes] => 1
[RailCostSaving] => 150
[Costsavingshipments] => 2
)
[origin_city] => Array
(
[origin_pcode] => 3170
)
[dest_city] => Array
(
[dest_pcode] => 4670
)
)
)
输出
array_merge_recursive()
。
我尝试下面的代码,但得到了我想要的不同结果......
$array3 = array_merge_recursive($content, $modecost);
echo "<pre>";
print_r($array3);
exit();
我希望所有 u_data 字段都在一个数组中,而不是在不同的数组中。
Array
(
[0] => Array
(
[u_data] => Array
(
[Mode] => Rail
[TotalCost] => 150
)
[0] => Array
(
[CostSavingRoutes] => 1
[RailCostSaving] => 150
[Costsavingshipments] => 2
)
[origin_city] => Array
(
[origin_pcode] => 2046
)
[dest_city] => Array
(
[dest_pcode] => 4361
)
)
[1] => Array
(
[u_data] => Array
(
[Mode] => Rail
[TotalCost] => 150
)
[0] => Array
(
[CostSavingRoutes] => 1
[RailCostSaving] => 150
[Costsavingshipments] => 2
)
[origin_city] => Array
(
[origin_pcode] => 3170
)
[dest_city] => Array
(
[dest_pcode] => 4670
)
)
[2] => Array
(
[u_data] => Array
(
[totalcharge] => 15
[RoadTotal] => 15
)
)
[3] => Array
(
[u_data] => Array
(
[totalcharge] => 15
[RoadTotal] => 15
)
)
)
试试这个
$res = Hash::merge($arr1, $arr2);
正确使用array_merge_recursive。
$arr1 = array(
'0' => array(
'u_data' => array(
'Mode' => 'Rail',
'TotalCost' => 150,),
'0' => array(
'CostSavingRoutes' => 1,
'RailCostSaving' => 150,
'Costsavingshipments' => 2,),
'origin_city' => array(
'origin_pcode' => 2046,),
'dest_city' => array(
'dest_pcode' => 4361,),
),
'1' => array(
'u_data' => array(
'Mode' => 'Rail',
'TotalCost' => 150,),
'0' => array(
'CostSavingRoutes' => 1,
'RailCostSaving' => 150,
'Costsavingshipments' => 2,),
'origin_city' => array(
'origin_pcode' => 3170,),
'dest_city' => array(
'dest_pcode' => 4670,),
),
);
$arr2 =array(
'0' => array(
'u_data' => array(
'totalcharge' => 15,
'RoadTotal' => 15,)
),
'1' => array(
'u_data' => array(
'totalcharge' => 15,
'RoadTotal' => 15,),
)
);
for ($i=0; $i < count($arr1); $i++) {
$final[] = array_merge_recursive($arr1[$i], $arr2[$i]);
}
echo "<pre>".print_r($arr1,1)."</pre>";
echo "<pre>".print_r($arr2,1)."</pre>";
echo "<pre> Final ---> ".print_r($final,1)."</pre>";
我认为 array_merge_recursive 会起作用,但它似乎不是那样工作的。
<?php
$array1Entry1 = array(
'u_data' => array(
'Mode' => 'Rail',
'TotalCost' => 150
)
);
$array1Entry2 = array(
'u_data' => array(
'Mode' => 'Rail',
'TotalCost' => 150
)
);
$array2Entry1 = array(
'u_data' => array(
'totalcharge' => 15,
'RoadTotal' => 15
)
);
$array2Entry2 = array(
'u_data' => array(
'totalcharge' => 15,
'RoadTotal' => 15
)
);
$array1 = array($array1Entry1, $array1Entry2);
$array2 = array($array2Entry1, $array2Entry2);
$mergedArray = array();
foreach ($array1 as $key => $values) {
$mergedArray[$key] = array_merge_recursive($array1[$key], $array2[$key]);
}
这段代码给出了这个输出。
Array
(
[0] => Array
(
[u_data] => Array
(
[Mode] => Rail
[TotalCost] => 150
[totalcharge] => 15
[RoadTotal] => 15
)
)
[1] => Array
(
[u_data] => Array
(
[Mode] => Rail
[TotalCost] => 150
[totalcharge] => 15
[RoadTotal] => 15
)
)
)
尝试这个代码,需要很多时间才能找到解决方案,但我做到了......
<?php
$arr1 = array(
'0' => array('u_data' => array('Mode' => 'Rail','TotalCost' => 150),
'0' => array( 'CostSavingRoutes' => 1,'RailCostSaving' => 150,'Costsavingshipments' => 2),
'origin_city' => array('origin_pcode' => 2046),
'dest_city' => array('dest_pcode' => 4361),
),
'1' => array('u_data' => array('Mode' => 'Rail','TotalCost' => 150),
'0' => array( 'CostSavingRoutes' => 1,'RailCostSaving' => 150,'Costsavingshipments' => 2),
'origin_city' => array('origin_pcode' => 3170),
'dest_city' => array('dest_pcode' => 4670),
),
);
$arr2 =array(
'0' => array('u_data' => array('totalcharge' => 15,'RoadTotal' => 15)),
'1' => array('u_data' => array('totalcharge' => 15,'RoadTotal' => 15))
);
$newArray =array();
foreach($arr1 as $key => $val)
{
$newArray[] = array_merge_recursive($val,$arr2[$key]);
}
echo "<pre>"; print_r($newArray);
?>
这将输出:
Array
(
[0] => Array
(
[u_data] => Array
(
[Mode] => Rail
[TotalCost] => 150
[totalcharge] => 15
[RoadTotal] => 15
)
[0] => Array
(
[CostSavingRoutes] => 1
[RailCostSaving] => 150
[Costsavingshipments] => 2
)
[origin_city] => Array
(
[origin_pcode] => 2046
)
[dest_city] => Array
(
[dest_pcode] => 4361
)
)
[1] => Array
(
[u_data] => Array
(
[Mode] => Rail
[TotalCost] => 150
[totalcharge] => 15
[RoadTotal] => 15
)
[0] => Array
(
[CostSavingRoutes] => 1
[RailCostSaving] => 150
[Costsavingshipments] => 2
)
[origin_city] => Array
(
[origin_pcode] => 3170
)
[dest_city] => Array
(
[dest_pcode] => 4670
)
)
)