我希望数组中的每个数组都显示在li中,这样3个子数组就有3 <ul>
个。但是,每个子数组的第二个和第三个值必须在同一个<li>
内。像这样:
natraj :
HB : Rs.10,
HH : Rs.12
我使用了一个for循环,其输出是这样的:
Natraj
HB
10
HH
12
以及其他子阵列等等。请给出类似于我使用的代码,以便我能更好地理解这一点。我使用的代码是:
<?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>";
?>
提前致谢
答案 0 :(得分:1)
您可以这样做:
$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>';
答案 1 :(得分:0)
尝试将内循环更改为:
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>";
}
答案 2 :(得分:0)
为什么不像这样更改内部for循环:
<?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>";
}
?>
答案 3 :(得分:0)
我认为这是我想要的输出(基于@syed qarib的想法,但很少修改):
$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>";
}
全部谢谢!