2015-08-17 368 views
-2

我動態形式部分正在與一個幸福添加重複的場,用於序列化這一點,我想兩個或更多陣列的組合陣列,現在用兩個陣列測試和我結合這兩個這樣的,如何組合兩個多個數組?

$array1 = Array 
(
    [0] => Collect Invoices 
    [1439797510547] => Array 
    (
     [0] => Bank Name 
     [1] => Invoices 
    ) 

    [1] => Comment 
); 
$array2 = Array 
(
    [0] => repeatField 
    [1439797510547] => Array 
    (
     [0] => selectBanks 
     [1] => text 
    ) 

    [1] => textarea 
); 
$mixedArray = array_combine($array1,$array2); 

/* Result is like this */ 
Array 
(
    [Collect Invoices] => repeatField 
    [Array] => Array 
    (
     [0] => selectBanks 
     [1] => text 
    ) 

    [Comment] => textarea 
) 

但我想這樣的一個答案

Array 
(
    [Collect Invices] => repeatField 
    [1439797510547] => Array 
    (
     [Bank Name] => selectBanks 
     [Invoices] => text 
    ) 

    [Comment] => textarea 
) 

誰能幫我,提前please..thanks

+2

你至今嘗試過什麼? –

+1

你想要什麼沒有魔法。你需要循環併合並/合併需要的東西。 –

+0

「銀行名稱」,「發票」,「評論」是常量名稱字段嗎?它與其他數組的值是否有直接聯繫? – Justinas

回答

1

在這裏你去隊友:

$array1 = array(
      0 => 'Collect Invoices', 
      1439797510547 => array(
       0 => 'Bank Name', 
       1 => 'Invoices' 
      ), 
      1 => 'Comment' 
     ); 
     $array2 = array 
      (
      0 => 'repeatField', 
      1439797510547 => array 
       (
       0 => 'selectBanks', 
       1 => 'text' 
      ), 
      1 => 'textarea' 
     ); 


     $result_array = array(); 

     foreach ($array1 as $key => $value) { 
      if (!is_array($value)) { 
       $result_array[$value] = $array2[$key]; 
      } 

      if (is_array($value)) { 
       foreach ($value as $inner_key => $inner_value) { 
        $result_array[$key][$inner_value] = $array2[$key][$inner_key]; 
       } 
      } 
     } 

     echo "<pre>"; 
     print_r($result_array); 
     echo "</pre>"; 

結果是:

Array 
(
    [Collect Invoices] => repeatField 
    [983466387] => Array 
     (
      [Bank Name] => selectBanks 
      [Invoices] => text 
     ) 

    [Comment] => textarea 
) 
+0

非常感謝你hmtareque,它的工作很好 –

+0

歡迎隊友:) –