Php 如何在for each循环中添加计数

Php 如何在for each循环中添加计数,php,google-maps,Php,Google Maps,我想弄清楚我怎样才能加上计数,这样每个结果都能像a-b-c这样显示出来。。但我不知道我需要做什么,你能帮我吗。为了让它更清晰,我尝试添加谷歌地图在你搜索时的功能,它会根据搜索结果显示标记和a、b、c $xmlString = file_get_contents($mapURL); $xmlString = str_replace('georss:point','point',$xmlString); $xml = new SimpleXMLElement($xmlString); $items

我想弄清楚我怎样才能加上计数,这样每个结果都能像a-b-c这样显示出来。。但我不知道我需要做什么,你能帮我吗。为了让它更清晰,我尝试添加谷歌地图在你搜索时的功能,它会根据搜索结果显示标记和a、b、c

$xmlString = file_get_contents($mapURL);
$xmlString = str_replace('georss:point','point',$xmlString);

$xml = new SimpleXMLElement($xmlString);
$items = $xml->xpath('channel/item');
$closeItems = array();
foreach($items as $item)
{
    $latlng = explode(' ',trim($item->point));

    //calculate distance
    $dist = calc_distance(array('lat'=>$zlat,'long'=>$zlng),array('lat'=>$latlng[0],'long'=>$latlng[1]));
    if($dist<={segment_4})$closeItems[] = $item;
}

?>
<div style="float:left; width:100%;margin:10px 0px;"><h1 style="font-size:16px; font-weight:bold; border-bottom:1px solid #333;">Servicer Results (within {segment_4} miles)  of Your selected area</h1></div>
$xmlString=file\u get\u contents($mapURL);
$xmlString=str_replace('georss:point','point',$xmlString);
$xml=新的simplexmlement($xmlString);
$items=$xml->xpath('channel/item');
$closeItems=array();
foreach($items作为$item)
{
$latlng=爆炸(“”,修剪($item->point));
//计算距离
$dist=计算距离(数组('lat'=>$zlat,'long'=>$zlng),数组('lat'=>$latlng[0],'long'=>$latlng[1]);
如果($dist)
所选区域的服务器结果(在{segment_4}英里内)
//有问题的代码

 <?php foreach($closeItems as $item ):?> 
            <div style="float:left; width:275px;height:auto;margin:20px 5px;margin-left:40px; font-family: Arial,Helvetica;font-size:12px">
        <h2 style="font-size:16px;color: #499AAE; ;"><img src="http://winstonind.com/images/mapicon.png" alt="map icon" />&nbsp;<strong><?=$item->title?></strong></h2>
    </div>


新代码可以工作,但不能作为字母

 <div style="float:left; width:100%;margin:10px 0px;"><h1 style="font-size:16px; font-weight:bold; border-bottom:1px solid #333;">Servicer Results (within {segment_4} miles)</h1></div>
<?php foreach($closeItems as $index=>$item ):?> 

    <div style="float:left; width:275px;height:auto;margin:20px 5px;margin-left:40px; font-family: Arial,Helvetica;font-size:12px">
    <h2 style="font-size:16px;color: #499AAE; ;"><img src="http://winstonind.com/images/mapicon.png" alt="map icon" />&nbsp;<strong><? echo $index; ?><?=$item->title?></strong></h2>
</div
Servicer结果(在{segment_4}英里内)


使用ascii码与

更新以下代码并检查手册中的
chr()
,这应该是您所需要的

...
$ascii = 97;
foreach($items as $item) {
  $letter = chr($ascii++);
  echo $letter;
  ...

注意:如果您使用的是值,这是一个PHP解决方案。但是,如果这是严格用于显示,Jimmy Sawczuk有一个很好的CSS解决方案。

您可以按顺序列表打印结果:

<ol type="a">
    <? foreach($closeItems as $item ):?> 
        <li><!-- whatever --></li>
    <? endforeach; ?>
</ol>


  • foreach
    有一个计数器,指示您所处的位置:

    <?php foreach($closeItems as $index=>$item ):?> 
    //now just use $index as your counter
    
    
    //现在只需使用$index作为计数器
    
    试试看

    
    
    
    echo chr($i+65)+':“+$closeItems[$i]->title;这将在26个项目后中断提醒你;)我用你的一些代码更新了帖子,但它开始时是0,1,2,3。我重新编辑了我的帖子,请再试一次。请注意,我无法从im所在的位置进行php调试;)
     <?php 
          $ascii = 65;
          for($i = 0; $i < sizeof($closeItems); ++$i){ ?> 
        <div style="float:left; width:275px;height:auto;margin:20px 5px;margin-left:40px; font-family: Arial,Helvetica;font-size:12px">
            <h2 style="font-size:16px;color: #499AAE; ;"><img src="http://winstonind.com/images/mapicon.png" alt="map icon" />&nbsp;<strong><? echo chr($ascii) + ': ' + $closeItems[$i]->title; $ascii++; ?></strong></h2>
        </div>
     <? } ?>