我有多維數組,我在foreach循環中回顯了鍵和值。我想有旁邊的「ID」(密鑰)來更新id的值,並也在旁邊「尺寸」(密鑰)的更新包來更新大小值在foreach循環中更新多維數組
問題 :: 貝婁我的代碼,輸入框回顯出每個鍵的正確值,但是當我點擊更新按鈕時,它會更新...
更新並不一定在foreach循環,順便說一句。我只是認爲這會更容易
預先感謝所有幫助
CODE
<?php
session_start();
$array=array(
'Homer' => Array
(
'id' => 111,
'size' => 54
),
'Marge' => Array
(
'id' => 222,
'size' => 12
),
'Bart' => Array
(
'id' => 333,
'size' => 3
)
);
// update if submit
if (isset($_POST["submit"]))
{
// i tired a number of things here but it was all errors
}
echo "<form method='post' action=''>";
// put the array in a session variable
if(!isset($_SESSION['simpsons']))
$_SESSION['simpsons']=$array;
// getting each array in a foreach loop
foreach($_SESSION['simpsons'] as $character => $info) {
echo $character.': id is '.$info['id'].', size is '.$info['size'];
//add and update input box for each ' id ' and ' size '
?>
<!-- input for id -->
<input name="<?php $character ?>" value="<?php echo $info['id'] ?>">
<!-- input for size-->
<input name="<?php $character ?>" value="<?php echo $info['size'] ?>">
<?php
echo"<br/>";
}
?>
<!-- submit button for the form -->
<input class="inputbox" type="submit" value="Update value of key" name="submit"/>
</form>
你剛纔忘了寫'echo'在您輸入'name'屬性。並需要將'name'屬性設置爲數組。所以你輸入的標籤看起來像是 []」value =「<?php echo $ info ['id']?>」>'。 –
@hardiksolanki即使他做了,他也會有兩個同名的輸入元素,這些元素不是無線電,所以從技術上講,一個元素會被覆蓋。他必須使用'[]'括號 –
@FlorianMüller檢查我更新的答案。 –