0

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.

4
  • 1
    how is the value for the checkboxes being determined? Commented Jul 8, 2013 at 6:53
  • @Orangepill sorry what? Commented Jul 8, 2013 at 6:55
  • vale attributes for the check boxes are 1,2,1,2 .... how are these vales being arrived at Commented Jul 8, 2013 at 7:00
  • @Orangepill that is static value and not important at this stage. Commented Jul 8, 2013 at 7:03

4 Answers 4

2

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>";
    }
    ?>
Sign up to request clarification or add additional context in comments.

Comments

2

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>';
}

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.