2017-04-02 75 views
0

我有下面的數組2d,變量($ C00等)本身有價值。如何在PHP中調用數組2D?

$matriks = array(array($C00, $C01, $C02, $C03), 
      array($C10, $C11, $C12, $C13), 
      array($C20, $C21, $C22, $C23), 
      array($C30, $C31, $C32, $C33)); 

我想使用二維數組下一頁上,以提交表單POST方法: 我不知道該代碼corret或不:)。

foreach ($matriks as $val) { 
foreach ($val as $v) { 
echo '<input type="hidden" name="Matriks[][]" value="'.$v.'">'; 
}} 

那麼如何在action頁面調用它呢? (提交後) 我這樣的代碼,但仍然是錯誤的。

$matriks=array(); 
foreach($_POST['Matriks'] as $key => $val) { 
foreach ($val as $k => $v) { 
    $matriks[$key][$k] = $v; 
}} 
+1

你能善意地一點更清楚..? –

回答

0

更新如圖所示的這部分代碼:

foreach ($matriks as $i => $val) { 
    foreach ($val as $v) { 
     echo '<input type="hidden" name="Matriks[' . $i . '][]" value="'.$v.'">' . "\n"; 
    } 
} 

使用$i鍵確保您的矩陣在DOM很好的體現,否則所有的值將只在位置互相覆蓋,其他[0, 0]

如果您在操作頁面上var_dump$_POST,你會看到你這樣的矩陣輸出:

array (size=1) 
    'Matriks' => 
    array (size=4) 
     0 => 
     array (size=4) 
      0 => string '00' (length=2) 
      1 => string '01' (length=2) 
      2 => string '02' (length=2) 
      3 => string '03' (length=2) 
     1 => 
     array (size=4) 
      0 => string '10' (length=2) 
      1 => string '11' (length=2) 
      2 => string '12' (length=2) 
      3 => string '13' (length=2) 
     2 => 
     array (size=4) 
      0 => string '20' (length=2) 
      1 => string '21' (length=2) 
      2 => string '22' (length=2) 
      3 => string '23' (length=2) 
     3 => 
     array (size=4) 
      0 => string '30' (length=2) 
      1 => string '31' (length=2) 
      2 => string '32' (length=2) 
      3 => string '33' (length=2) 
+0

很好,先生,非常感謝你。它解決了 – LDV

+0

不客氣:)請接受答案 –