簡體   English   中英

想要對數組中的每個鍵進行計數並將其分隔為變量以為其分配功能

[英]want to count and separate each key in the array to a variable to assign them functions

我正在為每個循環使用一個鍵值對以獲取后數據,現在我想分別捕獲下面的岩石數組中的每個元素,以便為它們分配不同的功能

<input type = "text" name = "rock[bonjovi manson mettalica]">
foreach ($_POST as $key => $value) {
foreach($value as $k => $v){
echo '<p>'.$k.'</p>'; // this echo's all elements in the array, i would like to get each element in the array so i could assign them the below functions
if ($k === "bonjovi"){
//do something
}
if ($k === "manson"){
//do something
}
if ($k === "mettalica"){
//do something
}
}
}

在輸入的名稱(如rock[]加上方括號表示提交表單時php的數組; 在方括號內添加文本可為數組的元素分配名稱:

<input type="text" name="rock[ a b c ]">

$ _ POST

array(1) {
  ["rock"]=>
  array(1) {
    [" a b c "]=>
    string(5) "d e f"
  }
}

也許您想要復選框嗎? 或此處的其他示例https://secure.php.net/manual/en/faq.html.php#faq.html.select-multiple

<input type="checkbox" name="rock[a]" value="A"> A 
<input type="checkbox" name="rock[b]" value ="B"> B
<input type="checkbox" name="rock[c]" value="C"> C
<input type="submit">

$ _ POST

array(1) {
  ["rock"]=>
  array(2) {
    ["a"]=>
    string(1) "A"
    ["c"]=>
    string(1) "C"
  }
}

foreach ($_POST as $key => $value)
$_POST包含所有POST數據,您可能需要嘗試
foreach ($_POST["rock"] as $key => $value)僅訪問“ rock”中的內容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM