[英]PHP: foreach in multidimensional arrays
我有一個動態表單來創建創建數組的章節和子章節:
后續代碼var_dump($ _ POST);
array{["textfield"] => array {
[0] => "title one"
[1] => "title two"
[2] => "title three"
[4] => "title four"
}
["textarea"] => array {
[0] => "title text"
[1] => "title summary"
[2] => "title description"
[4] => "title details"
}
["hidden"] => array {
[0] => "1"
[1] => "2"
[2] => "3"
[4] => "1"
}
}
我對數組非常弱。 我已經閱讀了幾篇關於多維數組和排序的文章,但沒有運氣或看到任何類似於我的例子,讓我了解我需要如何調整它。
我想為每個人:
<div class="row<? echo $hidden ?>">
<h2><? echo $textfield ?></h2>
<h3><? echo $textarea ?></h3>
</div>
通過幾個數組匹配鍵0(或相應的鍵號)和值。 相近:
<div class="row<? echo $_POST['hidden'][0] ?>">
<h2><? echo $_POST['textfield'][0] ?></h2>
<h3><? echo $_POST['textarea'][0] ?></h3>
</div>
<div class="row<? echo $_POST['hidden'][1] ?>">
<h2><? echo $_POST['textfield'][1] ?></h2>
<h3><? echo $_POST['textarea'][1] ?></h3>
</div>
<div class="row<? echo $_POST['hidden'][2] ?>">
<h2><? echo $_POST['textfield'][2] ?></h2>
<h3><? echo $_POST['textarea'][2] ?></h3>
</div>
<div class="row<? echo $_POST['hidden'][3] ?>">
<h2><? echo $_POST['textfield'][3] ?></h2>
<h3><? echo $_POST['textarea'][3] ?></h3>
</div>
這個表單可以動態創建數百個深度,我只能打印整個數組或每個$ key的所有$值。 我沒有通過各種數組匹配任何成功。
我希望你跟進。 如果您有任何建議,我將非常感激。
在模板中:
<? for ($i = 0; $i < count($_POST['textfield']); $i++): ?>
<div class="row<? echo $_POST['hidden'][$i] ?>">
<h2><? echo $_POST['textfield'][$i] ?></h2>
<h3><? echo $_POST['textarea'][$i] ?></h3>
</div>
<? endfor; ?>
雖然這將工作得很好(假設每個嵌套數組的長度相同),我認為構建數組的更合適的方法是:
array{
[0] => array{
["hidden"] => "1"
["textarea"] => "title text"
["textfield"] => "title one"
}
[1] => array{
["hidden"] => "2"
["textarea"] => "title summary"
["textfield"] => "title two"
}
}
然后你的循環看起來像:
<? foreach ($array as $chapter): ?>
<div class="row<? echo $chapter['hidden'] ?>">
<h2><? echo $chapter['textfield'] ?></h2>
<h3><? echo $chapter['textarea'] ?></h3>
</div>
<? endforeach; ?>
假設$_POST
中的每個元素與其他元素具有相同的長度,您可以使用count()
來獲取其中一個元素(例如textfield
)的長度並執行for
循環。
$length = count($_POST['textfield']);
for ($i = 0; $i < $length; $i++)
{
echo $_POST['textfield'][$i];
}
如果你知道數組鍵,你可以在這里使用for循環:
for($i=0;$i<count($array);$i++){
printf('<div class="row%s"><h2>%s</h2><h3>%s</h3></div>%s',$_POST['hidden'][$i],$_POST['textfield'][$i],$_POST['textarea'][$i],PHP_EOL);
}
Simples!
這就是你需要的:
<?php
// $x = %your_array
foreach ($x['hidden'] as $k => $v) {
?>
<div class="row<? echo $x['hidden'][$k] ?>">
<h2><? echo $x['textfield'][$k]; ?></h2>
<h3><? echo $x['textarea'][$k]; ?></h3>
</div>
<?php
}
?>
如果你確實想要出於語義原因在模板中做foreach,你可以事先做一些額外的工作來交換數組的維度,如:
$rows = array();
for ($i = 0; $i < count($_POST['textfield']); $i++) {
$rows[i]['title'] = $_POST['textfield'][$i];
$rows[i]['text'] = $_POST['textarea'][$i];
$rows[i]['hidden'] = $_POST['hidden'][$i];
}
然后你就可以在你的模板中做一個foreach,如下所示:
<?php foreach ($rows as $row) { ?>
<div class="row<? echo $row['hidden'] ?>">
<h2><? echo $row['title'] ?></h2>
<h3><? echo $row['text'] ?></h3>
</div>
<?php } ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.