2016-04-11 7 views
1

私は2つの配列を持っています。配列1と配列2です。 どちらの配列にも2つのレコードがあります。複数の配列をPHPでマージする

今、私は1つのアレイ内のアレイの両方をマージする必要があります。..

私はU_DATAから2つのフィールドの下に取得しています私の二番目の配列で。

[totalcharge] => 15 
[RoadTotal] => 15 

今私は、最初の配列で、このフィールドをマージする必要があります。..

私を助けてください、私は複数のアレイをマージする必要があります。以下は問題です:

私はこのような何かを試してみました...しかし、私は私の予想出力を達成することはできません。..

$abc = array_merge($content,$modecost); 

**Array 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 
       ) 

     ) 

) 


**Array 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(); 

私はarray_merge_recursiveが働くだろうと思ったが、そのように動作していないよう

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 
       ) 

     ) 

) 
+0

try 'array_merge_recursive' –

+0

こんにちは私は私の質問でarray_merge_recursiveの出力を追加しました......しかし、私はwant..plzヘルプとして私の結果を得ていません.. –

+0

@ testdesk私の答えをチェックしてください。 – Monty

答えて

1

適切な方法で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>"; 
+0

ありがとうございました。あなたの助け..... –

0

...別のアレイ内の1つの配列でない内のすべてのU_DATAフィールドをしたいです。

<?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]); 
} 

このコードはこの出力を示します。

+0

こんにちは私の質問にarray_merge_recursiveの出力を追加しました......しかし、私の望む結果が得られません... –

2

このコードを試してみてください。この

$res = Hash::merge($arr1, $arr2); 
+0

@testこんにちはこれを試してください –

+0

'Set :: merge()'の代わりに 'Hash :: merge()'を使ってください。 – drmonkeyninja

+0

はいこれも使うことができます –

0

を試してみて、それは解決策を見つけるために多くの時間がかかりますが、私はそれをやった...

<?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 
       ) 

     ) 

)