Tommy
Tommy

Reputation: 79

PHP: foreach in multidimensional arrays

I have a dynamic form for creating chapters and sub-chapters that creates 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'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.

I would like to for each:

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

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.

Upvotes: 3

Views: 1367

Answers (5)

Max Spencer
Max Spencer

Reputation: 1719

If you did want to do a foreach in the template for semantic reasons, you could so some extra work beforehand to swap the dimensions of the array like:

$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 could do a foreach in your template like so:

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

Upvotes: 0

d4rkpr1nc3
d4rkpr1nc3

Reputation: 1827

is this 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
}
?>

Upvotes: 0

clexmond
clexmond

Reputation: 1549

In a template:

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

Upvotes: 3

Marc
Marc

Reputation: 139

You could just use a for loop here instead if you know the array keys:

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!

Upvotes: 1

Fad
Fad

Reputation: 9858

Assuming that each element in $_POST has the same length as the others, you can use count() to get the length of one of the elements (e.g. textfield) and do a for loop.

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

Upvotes: 2

Related Questions