【问题标题】:Issues while displaying Arrays php显示数组php时的问题
【发布时间】:2017-07-12 12:56:12
【问题描述】:

我对 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 完全陌生

【问题讨论】:

  • 你遇到了什么错误?
  • @PrafullaKumarSahu:i dint 得到我期望的结果..这里所有的值都回显文本区域和输入字段..我需要 A B C D 输入字段和 37 40 ...textarea 请帮助我
  • 您是否能够重组您的数组,因为目前它的格式不适合您这样做?
  • @PeterFeatherstone:我该怎么做?请帮帮我
  • 为什么要将输入文本的值设为动态,而将 textarea 设为静态?

标签: php html arrays


【解决方案1】:

试试这个:根据你的数组

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

【讨论】:

  • 使用增量的 foreach 是什么意思?为什么不使用 simple for?
  • 感谢您的回复@mayank
【解决方案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 } ?>

【讨论】:

  • :thankyeww..如果不重新排列这个数组..我能得到答案吗?我的方法错了吗?
  • 下面还有其他答案可以这样做,但在我看来它们太可怕了。恐怕它们是不可维护的、不可扩展的、不必要的特定和复杂的……总是首先改进你的数据结构……
【解决方案3】:

这样试试。

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

【讨论】:

  • 如果将来使用非数值(如空白)或未知字符串表示(如NA)怎么办?
  • 根据当前数组和要求,如果将来数组发生变化,这个逻辑就可以了,那么场外逻辑就会改变。
  • 换句话说,This approach is not maintainable nor scalable?
  • 如果您需要它更具可扩展性,请将您的阵列重新创建为类似 $age = [ [ 'name' =&gt; 'A', 'value' =&gt; 'test', 'type' =&gt; 'textInput' ], [ 'name' =&gt; 'Arating', 'value' =&gt; '37', 'type' =&gt; 'textarea' ], ];
  • @PeterFeatherstone 根据当前的数组结构,只有这种可能性,否则您必须通过另一个 $count 来检查。在这些数组结构上没有任何可扩展的方式。
【解决方案4】:
<?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 } ?>

【讨论】:

  • 如果他将来有AA的密钥会怎样?
  • 您需要做的就是检查密钥的长度以确定您是否要显示文本区域或输入字段。只要您的密钥在文本字段中保留 1 个字符,这将起作用。
  • 如果你的一个键有AA,你总是可以添加一个$count变量; $计数=1; foreach... if(count == 2){ count++; endforeach
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-07
  • 2021-09-28
  • 2020-09-07
  • 2020-06-20
相关资源
最近更新 更多