PHP: foreach in multidimensional arrays

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

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 am very weak with arrays. I read several articles on multidimensional arrays and sorting, but havent been lucky or have seen examples similar to mine enough to understand how I need to adjust this.

I would like for everyone:

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

which corresponds to key 0 (or the 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 can be dynamically created in hundreds of depths, and I was able to print the entire array or all the $ values ​​for each key $. Ive not succeeded in various arrays.

I hope you go. If you have any suggestions, I will be very grateful.

+3
source share
5

:

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

, $_POST , , count() (, textfield) for.

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

Instead, you can use the for loop here if you know the keys of the array:

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!

+1
source

This is what you need:

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

If you want to make foreach in the template for semantic reasons, you could do some extra work to replace the dimensions of the array, for example:

$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];
}

Then you can do foreach in your template as follows:

<?php foreach ($rows as $row) { ?>
    <div class="row<? echo $row['hidden'] ?>">
        <h2><? echo $row['title'] ?></h2>
        <h3><? echo $row['text'] ?></h3>
    </div>
<?php } ?>
0
source

All Articles