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

时间:2013-06-07 10:51:09

标签: php foreach counter increment

我有一个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="">

4 个答案:

答案 0 :(得分:1)

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

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

之后你可以在你的foreach循环中自由使用$ c。

答案 1 :(得分:0)

$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;
}

答案 2 :(得分:0)

我没有看到任何问题只是添加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;
 }

答案 3 :(得分:0)

使用可以使用计数器

    <?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();
?>