[英]Issues while displaying Arrays php
我是非常新的PHP,我試圖自己學習PHP。 我有一個陣列
<?php
$age=array("A"=>"test",
"Arating"=>"37",
"B"=>"test2",
"Brating"=>"40",
"c"=>"test3",
"crating"=>"41",
"D"=>"test4",
"Drating"=>"42");
?>
我想從像Expected output這樣的數組創建一個表單:
<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>
這里ABCD
將是輸入類型值,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的新手
試試這個:根據你的陣列
<?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>
因為它是你的數組沒有優化這樣做。 從技術上講,你可以使用modulos運算符%
進行一些檢查,並將第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 } ?>
試試這樣。
<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 } } ?>
<?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 } ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.