2017-08-08 21 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はこれを達成するためのクリーンな方法です。 Thumbs up :) – Kashyap

+0

'array_combine'は私を苛立たせます、配列が同じサイズであることは大変です。ただ言って。 – ArtisticPhoenix

+0

問題の配列も同じサイズです。サイズが異なる場合、これはまったく別の作業です。 – Timurib