2017-08-08 35 views
1

我有疑問在PHP中我想設置一個關聯數組,其中包含鍵和值。 我有一個數組$頭和一個mutidimentional數組$數據如下:如何在php中設置多維數組中的鍵

$headers=(
    [0] => Testcase Name 
    [1] => Cell Name 
    [2] => Customer 
    [3] => Flops 
    [4] => Title 
    [5] => Status 
    [6] => Mfix CCR(open/close) 
    [7] => Scenerio-Brief Description 
    [8] => Expected Results 
    [9] => CCR Status 
    [10] => CCR No. 
    [11] => Remarks 
    [12] => Testcase Path 
) 

$data=(
    [0] => Array 
     (
      [0] => /a/b/c 
      [1] => 
      [2] => 
      [3] => 
      [4] => 
      [5] => Done 
      [6] => close 
      [7] => 2D Elastic with scanformat=parallel 
      [8] => No miscompares for both scan and logic tests 
      [9] => 
      [10] => 1716280 
      [11] => 
      [12] => 
     ) 

    [1] => Array 
     (
      [0] => /x/y/z 
      [1] => 
      [2] => 
      [3] => 
      [4] => 
      [5] => Done 
      [6] => close 
      [7] => 2D Elastic with scanformat=parallel & explicitshifts 
      [8] => No miscompares for both scan and logic tests 
      [9] => 
      [10] => 1717028 
      [11] => 
      [12] => 
     ) 

    [2] => Array 
     (
      [0] => /a/p/q 
      [1] => 
      [2] => 
      [3] => 
      [4] => 
      [5] => Done 
      [6] => 
      [7] => Error if explicitshifts greater than scan length 
      [8] => No miscompares for both scan and logic tests 
      [9] => 
      [10] => 
      [11] => 
      [12] => 
     ) 

    [3] => Array 
     (
      [0] => /s/m/p 
      [1] => 
      [2] => 
      [3] => 
      [4] => 
      [5] => Done 
      [6] => 
      [7] => 2D Elastic + wide 1 Masking with scanformat=parallel 
      [8] => No miscompares for both scan and logic tests 
      [9] => 
      [10] => 
      [11] => 
      [12] => 
     ) 

) 

我想設置數字鍵[0] ... [12]作爲$ headers數組的值。 意思是我想替換[0] .... [12]帶有$ header [0] .... $ headers [12]。

請提供解決方案。

回答

0
$result = array(); 
foreach($data as $key => $val){ 
    $temp = array(); 
    foreach($val as $k => $v){ 
     $temp[$header[$k]] = $v; 
    } 
    $result[] = $temp; 
} 
+0

它工作Kashyap.Thanks噸! – confused

+0

如果它適合你,你可以接受答案。 – Kashyap

2

使用array_combine

$dataWithKeys = []; 
foreach ($data as $row) { 
    $dataWithKeys[] = array_combine($headers, $row); 
} 
+0

同意,array_combine是實現這種更清潔的方式。豎起大拇指:) – Kashyap

+0

'array_combine'使我感到沮喪,挑剔的是數組的大小相同。只是說。 – ArtisticPhoenix

+0

問題中的數組也是相同的大小。如果大小不同,那麼這是一個完全不同的任務。 – Timurib