显示数组的问题php

时间:2017-07-12 12:56:12

标签: 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 textarea的:Arating,Brating,Crating,Drating 输出完全错误。我是php的新手

4 个答案:

答案 0 :(得分:3)

试试这个:根据您的数组

<?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>

答案 1 :(得分:2)

因为它是你的数组没有优化这样做。从技术上讲,您可以使用模运算符%进行一些检查,并将第一个和第二个选项与if语句等结合使用,但这会变得混乱,如果您有物品乱序或想要更改或添加新的将来你会选择一个充满痛苦的话语。

最好将数组重新排列成一个多维数组,然后循环遍历它并通过它的键名访问每个项目。从长远来看,它将使您的代码更易于理解和维护。

以下代码应该更适合您:

<?php

$ages = array(
  "A" => [
    "val" => "test",
    "name" => "Arating",
    "num" => "37"
  ],
  "B" => [
    "val" => "test2",
    "name" => "Brating",
    "num" => "40"
  ],
  "C" => [
    "val" => "test3",
    "name" => "crating",
    "num" => "37"
  ],
  "D" => [
    "val" => "test4",
    "name" => "Drating",
    "num" => "40"
  ]
);

foreach($ages as $key => $age){ ?>
   <input name="<?php echo $key ?>" value="<?php echo $age['val'] ?>" id="<?php echo $age['val'] ?>" />
   <textarea rows="4" cols="50" name="<?php echo $age['name'] ?>"><?php echo $age['num'] ?></textarea>
<?php } ?>

答案 2 :(得分:2)

尝试那样。

<form action="" name="test" method="post"/>
<?php
    foreach($age as $key => $value) { 
     if(!is_numeric($value) ) {
?>
 <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 } } ?>

答案 3 :(得分:1)

<?php


$age = [
    "A"       => "test",
    "Arating" => "37",
    "B"       => "test2",
    "Brating" => "40",
    "c"       => "test3",
    "crating" => "41",
    "D"       => "test4",
    "Drating" => "42"
];


?>
<form action="" name="test" method="post"/>
<?php
foreach ($age as $key => $value) {
    if (strlen($key) == 1) { ?>

        <input name="<?php echo $key ?>" value="<?php echo $value ?>" id="<?=$value;?>"/>

    <?php } else { ?>

        <textarea rows="4" cols="50" name="<?php echo $key ?>"><?php echo $value ?></textarea>
    <?php } ?>



<?php } ?>

如果你有钥匙的AA

,那么另一种选择
<?php
$count = 1;
foreach ($age as $key => $value) {
    if ($count == 1) { ?>

        <input name="<?php echo $key ?>" value="<?php echo $value ?>" id="<?= $value; ?>"/>

    <?php } elseif($count==2) { ?>

        <textarea rows="4" cols="50" name="<?php echo $key ?>"><?php echo $value ?></textarea>
    <?php } ?>

    <?php
    if ($count == 2) {
        $count = 0;
    }
    $count++;

    ?>
<?php } ?>

更具伸缩性的解决方案

<?php


$age = [

    [
        'name'  => 'A',
        'value' => 'test',
        'type'  => 'textInput'
    ],
    [
        'name'  => 'Arating',
        'value' => '37',
        'type'  => 'textarea'
    ],
    [
        'name'  => 'B',
        'value' => 'test2',
        'type'  => 'textInput'
    ],
    [
        'name'  => 'Brating',
        'value' => '40',
        'type'  => 'textarea'
    ],
    [
        'name'  => 'c',
        'value' => 'test3',
        'type'  => 'textInput'
    ],
    [
        'name'  => 'crating',
        'value' => '41',
        'type'  => 'textInput'
    ],
    [
        'name'  => 'D',
        'value' => 'test4',
        'type'  => 'textInput'
    ],
    [
        'name'  => 'Drating',
        'value' => '42',
        'type'  => 'textInput'
    ],

];

?>
<form action="" name="test" method="post"/>
<?php

foreach ($age as $key => $value) {
    if ($value['type'] == 'textInput') { ?>

        <input name="<?php echo $value['name'] ?>" value="<?php echo $value['value'] ?>" id="<?= $value['value']; ?>"/>

    <?php } elseif ($value['type'] == 'textarea') { ?>

        <textarea rows="4" cols="50" name="<?php echo $value['name'] ?>"><?php echo $value['value'] ?></textarea>
    <?php } ?>


<?php } ?>