2012-01-24 85 views
0

我在$ _ POST一個有些價值是在有自己的數組中與此類似,

array(
    ['conviction_date'] = array(
     [0] => 12/01/2011 
     [1] => 22/12/2011 
    ) 
    ['conviction_description'] = array(
     [0] => This some text to show what the conviction was for etc. 
     [1] => This is some more text to show what the second convication was for 
    ) 
) 

我想知道什麼是我該怎麼辦環通說數組,以便我可以通過那些鍵來從一個值到另一個值的匹配?

是不是這樣簡單,

foreach ($_POST['conviction_date'] as $k => $v) { 
    $newArray[] = array(
     'conviction_date' => $v, 
     'conviction_details' => $_POST['conviction_details'][$k] 
    ) 
} 

這會不會再輸出的東西下面?

array(
    [0] => array(
     'conviction_date' => 12/01/2011 
     'conviction_details' => This is some to show what the conviction was for etc. 
    ), 
    [1] => array() 
     'conviction_date' => 22/11/2011 
     'conviction_details' => This is some more text to show what the second convication was for 
    ) 

有沒有建立一個更簡單的數組?

+1

你到處都是「定罪」和「定罪」(這不是一個真正的單詞)拼寫不匹配。所以開始了意大利麪條...... –

回答

1

編輯的形式向下列可能比較簡單:

<form method="post" action=""> 
<input type="text" name="conviction_date[]"> 
<input type="text" name="conviction_details[]"> 
</form> 

if (isset($_POST) { var_dump($_POST); } 
0

可能比較簡單給的參數名稱,這樣PHP將創建一個像

array('conviction'=>array(
    0=>array('date'=..., 'details'=>...), 
    1=>array('date'=..., 'details'=>...), 
    ... 
); 

一個數組,那麼你可以這樣做

foreach($_POST['conviction'] as $c) { 
    echo $c['date'], ' ', $c['details'], "\n"; 
} 

一個簡單的演示形式:

<form method="post" action="..."> 
    <fieldset><legend>0</legend> 
     <input type="text" name="conviction[0][date]"> 
     <input type="text" name="conviction[0][details]"> 
    </fieldset> 
    <fieldset><legend>1</legend> 
     <input type="text" name="conviction[1][date]"> 
     <input type="text" name="conviction[1][details]"> 
    </fieldset> 
    ... 
</form> 
0

您的代碼示例有一些錯誤,例如,定罪日期:12/01/2011應該可能是'12/01/2011'。

除此之外,您提出的解決方案似乎是合適的。或者,如果一組信念的每個日期都是唯一的(!),您也可以使用日期作爲密鑰(例如,UNIX時間戳或YYYYMMDD格式)。檢索數據將如此簡單:

foreach ($convictions as $date => $description) { ... }