Rajnikanth
Rajnikanth

Reputation: 2785

How to display multidimensional array with HTML

I have multidimensional array and I am getting output like this:

Array
(
    [Bukit Panjang LRT] => Array
        (
            [0] => Array
                (
                    [station_name] => Senja (BP13)
                )

            [1] => Array
                (
                    [station_name] => Ten Mile Junction (BP14)
                )
        )

    [Changi Airport Branch Line (CAL)] => Array
        (
            [2] => Array
                (
                    [station_name] => Changi Airport (CG2)
                )

            [3] => Array
                (
                    [station_name] => Expo (CG1 / DT35)
                )
        )
)

My foreach loop:

$level_keys = array();
foreach ($mrtlrt_line as $k => $sub_array){
  $this_level = $sub_array['header_name'];
  $level_keys[$this_level][$k] = array('station_name' => $sub_array['station_name']);
}

I want to display value in HTML element like this way:

<h4>Bukit Panjang LRT</h4> <div>
    <label class="label_check">
        <input name="line" value="1" type="checkbox" />Senja (BP13
    </label>
    <label class="label_check">
        <input name="line" value="2" type="checkbox" />Ten Mile Junction (BP14)
    </label> </div>

<h4>Changi Airport Branch Line (CAL)</h4> <div>
    <label class="label_check">
        <input name="line" value="1" type="checkbox" />Changi Airport (CG2)
    </label>
    <label class="label_check">
        <input name="line" value="2" type="checkbox" />Expo (CG1 / DT35)
    </label> </div>

Any ideas or suggestions? Thanks.

Upvotes: 0

Views: 262

Answers (4)

Hearaman
Hearaman

Reputation: 8726

Try this

    <?php
    foreach ($YourMulArr as $key => $subAry)
    {
        echo "<h4>" . $key . "</h4>"; 
        echo "<div>";
        foreach ($subAry as $k => $Ary)
        {
            echo "<label class='label_check'>";
            echo "<input name='line' value='" . $k+1 . "' type='checkbox' /> ". $Ary['station_name'];
            echo "</label>";
         }
         echo "</div>";
    }
    ?>

Upvotes: 2

bart_88
bart_88

Reputation: 488

To extend on swapnesh's answer, you want 2 foreach loops. Foreach mrtlrt_line you want to loop over whats inside that array.

You should be separating presentation logic from business logic as much as possible. It's a personal choice to use alt php syntax but I subjectively believe it makes presentation code easier to read.

<?php foreach($mrtlrt_line as $key => $value): ?>
<?php /* interator */  $i = 0; ?>
    <h4><?php echo $key; ?></h4>
    <div>
         <?php foreach($value as $innerkey=>$innerval): ?>
            <label class="label_check">
               <input name="line" value="<?php echo $i++; ?>" type="checkbox" />
               <?php echo $innernval; ?>
            </label>
        <?php endforeach; ?>
    </div>
<?php endforeach; ?>

Upvotes: 0

swapnesh
swapnesh

Reputation: 26732

Try this code

<?php
//Your array- `$mrtlrt_line`
foreach($mrtlrt_line as $key => $value)
{
   echo '<h4>',$key,'</h4><div>';
   foreach($value as $nkey=>$nval)
   {
      echo '<label class="label_check">';
      echo '<input name="line" value="1" type="checkbox" />',$nval;
      echo '</label>';
   }
   echo '</div>';
}

Upvotes: 2

Fahad Ajmal
Fahad Ajmal

Reputation: 1520

You will have to create html from your php foreach loop and inject the array values in it. see this code

foreach ($mrtlrt_line as $k => $sub_array){

  $level_keys[$this_level][$k] = array('station_name' => $sub_array['station_name']);
  $level_keys[$this_level][$k+1] = array('station_name' => $sub_array['station_name']);

  echo '<h4>'.$sub_array['header_name'].'</h4> <div><label class="label_check"><input name="line" value="1" type="checkbox" />'.$level_keys[$this_level][$k].'</label><label class="label_check"><input name="line" value="2" type="checkbox" />'.$level_keys[$this_level][$k+1].'</label> </div>';

}

i hope you will have an idea of what you should do to inject your array into your html.

Upvotes: 0

Related Questions