1

i want each of the arrays inside an array to be displayed inside an li such that 3 sub-arrays have 3 <ul>'s. However, the second and third value of each sub-array has to be inside the same <li>. like this:

natraj :
HB : Rs.10,
HH : Rs.12 

I used a for loop where the output is comin like this:

  Natraj
    HB
    10
    HH
    12

and so on for other sub-arrays too. Please give a code similar to the one i used so i can understand this better. The code i used is:

   <?php
 $pencils = array( array("Natraj", "HB", 10, "HH", 12), array("Apsara", "HB", 8, "HH", 9), array("Camlin", "HB", 11, "HH", 13)  );
    //natraj :
    //HB : Rs.10,
    //HH : Rs.12 

    for ($pencilSet=0; $pencilSet<3; $pencilSet++){
    //echo $pencils[$pencilSet];
    echo "<ul>";

    //echo "<strong>", $pencils[$pencilSet], "</strong>";
    for($pencilSetDetials=0; $pencilSetDetials<5; $pencilSetDetials++){
    echo "<li>",$pencils[$pencilSet][$pencilSetDetials], "</li>";
    }
    echo "</ul>";
?>

Thanks in advance

4 Answers 4

1

You can do something like this:

$pencils = array( array("Natraj", "HB", 10, "HH", 12), array("Apsara", "HB", 8, "HH", 9), array("Camlin", "HB", 11, "HH", 13)  );

echo '<style>ul{list-style-type: none;}</style>';
foreach($pencils as $data) {
    echo '<ul>';
    $heading = array_shift($data); // get the item name heading
    $values = array_chunk($data, 2); // group by two's
    echo "<li><strong>$heading</strong></li>";
    foreach($values as $value) {
        list($type, $price) = $value;
        echo "<li>$type: Rs.$price</li>";
    }
}
echo '</ul>';
Sign up to request clarification or add additional context in comments.

Comments

0

Try changing inner loop to this:

for($pencilSetDetials=0; $pencilSetDetials<5; $pencilSetDetials++){
    echo "<li>";
    echo  $pencils[$pencilSet][$pencilSetDetials];
    if($pencilSetDetials != 0){ //Donot run the condition if its the first element
         //Add 1 more the counter so we get the next value
         $pencilSetDetials++;
         //print the next value in the same li
         echo  ' : '.$pencils[$pencilSet][$pencilSetDetials];
    }
    echo  "</li>";
}

Comments

0

Why not to change the inner for loop like this :

<?php
$pencil = array
   (
   array('Natraj',HB,"10",HH,12),
   array("Apsara",HB,8,"HH",13),
   array("Camlin",HB,13,HH,13),

   );

for ($row = 0; $row <  3; $row++) {
   echo "<p><b>Row number $row</b></p>";
   echo "<ul>";
   for ($col = 0; $col <  3; $col++) {
     echo "<li>".$pencil[$row][$col]."</li>";
   }
   echo "</ul>";
}
?>

Comments

0

I think this is the output i wanted (based on the idea by @syed qarib, but little modifications):

$pencils = array( array("Natraj", "HB", 10, "HH", 12), array("Apsara", "HB", 8, "HH", 9), array("Camlin", "HB", 11, "HH", 13)  );
 for ($pencilSet=0; $pencilSet<3; $pencilSet++){

echo "<ul>";


for($pencilSetDetials=0; $pencilSetDetials<5; $pencilSetDetials++){

if($pencilSetDetials!=0){
echo "<li>" ,$pencils[$pencilSet][$pencilSetDetials], ": Rs." ;
$pencilSetDetials++;
}
echo $pencils[$pencilSet][$pencilSetDetials], "</li>";


}
echo "</ul>";

}

Thanks all!

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.