[英]want to count and separate each key in the array to a variable to assign them functions
I am doing a key value pair with a for each loop to get post data, now i would like to catch each element in the rock array below separately so i can assign different functions to them 我正在为每个循环使用一个键值对以获取后数据,现在我想分别捕获下面的岩石数组中的每个元素,以便为它们分配不同的功能
<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
}
}
}
Adding brackets to the Name of an input like rock[]
denotes an array for php when the form is submitted; 在输入的名称(如rock[]
加上方括号表示提交表单时php的数组; adding text inside the brackets assigns a name to the element of the array: 在方括号内添加文本可为数组的元素分配名称:
<input type="text" name="rock[ a b c ]">
$_POST $ _ POST
array(1) {
["rock"]=>
array(1) {
[" a b c "]=>
string(5) "d e f"
}
}
Maybe instead you want checkboxes? 也许您想要复选框吗? Or other examples here https://secure.php.net/manual/en/faq.html.php#faq.html.select-multiple 或此处的其他示例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 $ _ POST
array(1) {
["rock"]=>
array(2) {
["a"]=>
string(1) "A"
["c"]=>
string(1) "C"
}
}
foreach ($_POST as $key => $value)
$_POST
contains all POST data, in your case you might want to try $_POST
包含所有POST数据,您可能需要尝试
foreach ($_POST["rock"] as $key => $value)
to only access the content within 'rock' foreach ($_POST["rock"] as $key => $value)
仅访问“ rock”中的内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.