2012-04-19 37 views
3

我有一個創建章節和創建數組的子章節的動態表單:PHP:foreach在多維數組中

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的所有$值。我沒有任何成功通過各種陣列匹配。

我希望你跟着。如果您有任何建議,我會非常感激。

回答

2

假設在$_POST各元件具有相同的長度他人,可以使用count()來獲得元件中的一個(例如textfield)的長度,並做了for循環。

$length = count($_POST['textfield']); 
for ($i = 0; $i < $length; $i++) 
{ 
    echo $_POST['textfield'][$i]; 
} 
3

在模板中:

<? 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; ?> 
+0

@MaxSpencer,我其實是在編輯小號的東西imilar。我認爲改變陣列最初的構建方式是最有意義的。 – clexmond 2012-04-19 16:07:28

1

你可以只使用一個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!

0

這是你需要的東西:

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

假設它們長度相同,或者您將收到未定義的通知。 – MetalFrog 2012-04-19 16:23:08

0

如果你確實想要做的模板語義原因的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 } ?>