我是 php 的新手,我正在尝试自学 php。我有一个数组
<?php
$age=array("A"=>"test",
"Arating"=>"37",
"B"=>"test2",
"Brating"=>"40",
"c"=>"test3",
"crating"=>"41",
"D"=>"test4",
"Drating"=>"42");
?>
我想从数组创建一个表单 预期输出:
<html>
<form action="" name="test" method="post"/>
<input name="A" value="test" id="test"/>
<textarea rows="4" cols="50" name="Arating" >
37
</textarea>
<br>
<input name="B" value="test2" id="test2"/>
<textarea rows="4" cols="50" name="Brating" >
40
</textarea>
<br>
<input name="C" value="test3" id="test3"/>
<textarea rows="4" cols="50" name="Crating" >
41
</textarea>
<br>
<input name="D" value="test4" id="test4"/>
<textarea rows="4" cols="50" name="Drating" >
42
</textarea>
</form>
</html>
<html>
此处 A B C D
将是输入类型值,textarea 应始终为 Arating,Brating,Crating,Drating
我试过:
<form action="" name="test" method="post"/>
<?php
foreach($age as $key => $value){?>
<input name="<?php echo $key ?>" value="<?php echo $value ?>" id="test"/>
<textarea rows="4" cols="50" name="Arating" >
<?php echo $value ?>
</textarea>
<?php } ?>
始终输入名称:A,B,C,D
文本区域:Arating,Brating,Crating,Drating
输出完全错误。我是 php 的新手
最佳答案
试试这个:根据你的数组
<?php
$age=array("A"=>"test",
"Arating"=>"37",
"B"=>"test2",
"Brating"=>"40",
"c"=>"test3",
"crating"=>"37",
"D"=>"test4",
"Drating"=>"40");
?>
<form action="" name="test" method="post">
<?php
$i=0;
foreach($age as $key => $value)
{
if($i%2==0)
{?>
<input name="<?php echo $key ?>" value="<?php echo $value ?>" id="test"/>
<?php
}
else
{?>
<textarea rows="4" cols="50" name="<?php echo $key ?>" ><?php echo $value ?> </textarea>
<?php }
$i++;
} ?>
</form>
关于php - 显示数组 php 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45058286/