PHP:在的foreach多维数组 [英] PHP: foreach in multidimensional arrays

查看:108
本文介绍了PHP:在的foreach多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的章节和子章节创建数组的动态表单:

I have a dynamic form for creating chapters and sub-chapters that creates an array:

的var_dump($ _ POST);

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

我非常疲软,阵列。香港专业教育学院读多维数组几篇文章和排序,但还没有任何运气或看到任何例子不够紧密类似矿井让我明白,我怎么需要调整这一点。

I'm very weak with arrays. Ive read several articles on multidimentional arrays and sorting but havent had any luck or seen any examples closely enough resembling mine for me to understand how I need to adjust this.

我想每个:

<div class="row<? echo $hidden ?>">
    <h2><? echo $textfield ?></h2>
    <h3><? echo $textarea ?></h3>
</div>

这是0到多个阵列值(或相应的键号)和相匹配的关键。类似的:

that matches key 0 (or corresponding key number) and value through several arrays. Similar to:

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

这种形式可以动态创建数百个深,我只能够打印每个$键整个数组或全部$值。香港专业教育学院没有成功,通过不同的阵列相匹配。

This form could dynamically be created hundreds deep and I've only been able to print the entire array or all $values for each $key. Ive not had any success matching through various arrays.

我希望你能跟随。如果您有什么建议,我会非常感激。

I hope you follow. If you have any suggestions, I'd be very grateful.

推荐答案

在一个模板:

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

虽然这会是不错的(假设每个嵌套数组的长度相同),我认为建立阵列更合适的方法是这样的:

While this will work great (assuming each of the nested arrays are the same length) I think a more appropriate way to build your array would be something like:

array{
    [0] => array{
        ["hidden"]    => "1"
        ["textarea"]  => "title text"
        ["textfield"] => "title one"
    }
    [1] => array{
        ["hidden"]    => "2"
        ["textarea"]  => "title summary"
        ["textfield"] => "title two"
    }
}

然后你的循环是这样的:

Then your loop looks like:

<? foreach ($array as $chapter): ?>
<div class="row<? echo $chapter['hidden'] ?>">
    <h2><? echo $chapter['textfield'] ?></h2>
    <h3><? echo $chapter['textarea'] ?></h3>
</div>
<? endforeach; ?>

这篇关于PHP:在的foreach多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆