我的代码是这样的:
<?php
function romanic_number($integer, $upcase = true)
{
$table = array('M'=>1000, 'CM'=>900, 'D'=>500, 'CD'=>400, 'C'=>100, 'XC'=>90, 'L'=>50, 'XL'=>40, 'X'=>10, 'IX'=>9, 'V'=>5, 'IV'=>4, 'I'=>1);
$return = '';
while($integer > 0)
{
foreach($table as $rom=>$arb)
{
if($integer >= $arb)
{
$integer -= $arb;
$return .= $rom;
break;
}
}
}
return $return;
}
$testArray = array(
array(
'display' => '1', // strlen = 1 => it's Continent
'uraian' => 'European Continent',
),
array(
'display' => '114', // strlen = 3 => it's Country
'uraian' => 'England Country',
),
array(
'display' => '1141444', // strlen = 7 => it's City
'uraian' => 'London City',
),
array(
'display' => '1141445',
'uraian' => 'Manchester City',
),
array(
'display' => '1141452',
'uraian' => 'Liverpool City',
),
array(
'display' => '1141454',
'uraian' => 'Oxford City',
),
array(
'display' => '115',
'uraian' => 'Spain Country',
),
array(
'display' => '1151464',
'uraian' => 'Madrid City',
),
array(
'display' => '1151465',
'uraian' => 'Barcelona City',
),
array(
'display' => '2',
'uraian' => 'Asian Continent',
),
array(
'display' => '215',
'uraian' => 'Japan Country',
),
array(
'display' => '2151474',
'uraian' => 'Tokyo City',
),
array(
'display' => '2151475',
'uraian' => 'Osaka City',
)
);
echo '<table border=1>
<tr>
<th>No</th>
<th>Description</th>
</tr>';
$i = 1;
$j = 'A';
foreach($testArray as $key)
{
echo '<tr>
<td>';
if(strlen($key['display'])==3)
{
echo romanic_number($i);
++$i;
}
if(strlen($key['display'])==7)
{
echo $j;
++$j;
}
echo '</td>
<td>';
echo $key['uraian'];
echo '</td>
</tr>';
}
echo '</table>';
?>
结果如下:http://postimg.org/image/9jmn7mlr1/
我希望结果不是那样的,但是像这样:
https://postimg.org/image/579p02c1p/
我曾尝试编辑代码,但我仍然无法做到。
有没有人可以帮助我?
答案 0 :(得分:3)
你必须在这做两件事:
$i
完成一个洲的遍历后重置$j
和foreach
个变量。$j
。所以你的foreach
循环应该是这样的:
foreach($testArray as $key){
if(strlen($key['display'])==1){
$i = 1; $j = 'A';
}
echo '<tr><td>';
if(strlen($key['display'])==3){
echo romanic_number($i);
++$i;
$j = 'A';
}
if(strlen($key['display'])==7){
echo $j;
++$j;
}
echo '</td><td>';
echo $key['uraian'];
echo '</td></tr>';
}
答案 1 :(得分:1)
如果您的数组在构建html代码之前始终由Continent
/ Country
/ City
正确排序,那么您只需重置$i
/ {{1当你来到$j
Continent
放在循环的开头
if(strlen($key['display'])==1) {$i = 1;$j = 'A';}