PHP计数器:foreach中每个项目的增量ID


PHP Counter: Incremental ID for each item in foreach

我有一个foreach,我想有一个增量id="X" (X从1开始-说- 6,如果有6个项目存在)为每个项目。

代码如下:

<?php
function blahblah(){
    $url = 'THE_URL_WHERE_I_RETRIEVE_JSON';
    $cache = './BLAH/'.sha1($url).'.json';
    if(file_exists($cache) && filemtime($cache) > time() - 1000){
        $jsonData = json_decode(file_get_contents($cache));
    } else {
         $jsonData = json_decode((file_get_contents($url)));
        file_put_contents($cache,json_encode($jsonData));
    }
    $result = '<div id="my_div">'.PHP_EOL;
     if(is_array($jsonData->data)){
        // Do the for each
    } else {
        // It wasn't an array so do something else
    }
     foreach ($jsonData->data as $key=>$value) {
        $title = (!empty($value->caption->text))?' '.$value->caption->text:'...';
        $location = (!empty($value->location->name))?' at '.$value->location->name:null;
        $result .= "'t".'<a href="'.$value->images->standard_resolution->url.'">BLAHBLAH</a>'.PHP_EOL;
    }
    $result .= '</div>'.PHP_EOL;
     return $result;
}
echo get_instagram();
?>

例如我想把id="X"放在这里<a ID="" href="">

我觉得这没什么问题只要添加1个变量就可以作为计数器

 $i = 0;
 foreach ($jsonData->data as $key=>$value) {
    $title = (!empty($value->caption->text))?' '.$value->caption->text:'...';
    $location = (!empty($value->location->name))?' at '.$value->location->name:null;
    $result .= "'t".'<a id="'.++$id.'" href="'.$value->images->standard_resolution->url.'">BLAHBLAH</a>'.PHP_EOL;
 }

如果有理由不使用for循环,请使用计数器变量

$c = 1;
foreach($data as $item) {
   // code
  $c++; //increment the counter at the end of loop
}

你可以在foreach循环中自由使用$c

$idx = 1;
foreach ($jsonData->data as $key=>$value) {
        $title = (!empty($value->caption->text))?' '.$value->caption->text:'...';
        $location = (!empty($value->location->name))?' at '.$value->location->name:null;
        $result .= "'t".'<a id="'.$idx++ .'" href="'.$value->images->standard_resolution->url.'">BLAHBLAH</a>'.PHP_EOL;
}

use可以为这个

使用计数器
    <?php
    function blahblah(){
     $url = 'THE_URL_WHERE_I_RETRIEVE_JSON';
    $cache = './BLAH/'.sha1($url).'.json';
      if(file_exists($cache) && filemtime($cache) > time() - 1000){
      $jsonData = json_decode(file_get_contents($cache));
    } else {
     $jsonData = json_decode((file_get_contents($url)));
     file_put_contents($cache,json_encode($jsonData));
    }
    $result = '<div id="my_div">'.PHP_EOL;
     if(is_array($jsonData->data)){
        // Do the for each
    } else {
    // It wasn't an array so do something else
    }
    $i=1;//use as counter
    foreach ($jsonData->data as $key=>$value) {
    $title = (!empty($value->caption->text))?' '.$value->caption->text:'...';
    $location = (!empty($value->location->name))?' at '.$value->location->name:null;
    $result .= "'t".'<a '.'id="'.$i.'" href="'.$value->images->standard_resolution-   >url.'">BLAHBLAH</a>'.PHP_EOL;
  $i++;
}
$result .= '</div>'.PHP_EOL;
 return $result;
}
  echo get_instagram();
?>