php - 显示数组 php 时出现问题

标签 php html arrays

我是 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/

相关文章:

php - Doctrine 查询错误未知列

jquery - 使用 jQuery 预加载 CSS Sprite 图像

c - 段错误 : 11, C 绝对初学者指南,第 3 版 Chapter6ex1.c

java - 在java中查找pgm文件中二维数组的平均值

php - 从多个具有限制的多对多关系表中检索结果

php - Magento:如何获取 Paypal 交易 ID?

php - 使用 jquery 刷新动态 php div

javascript - 以编程方式检查网络资源是否已加载到网页

javascript - 如何自动单击 href 以选择内容

arrays - 如何在 Vue 2 组件的数组中添加和删除项目