1

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

4 回答 4

1

我没有看到任何问题,只需添加 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;
 }
于 2013-06-07T10:55:10.617 回答
1

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

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

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

于 2013-06-07T10:55:18.397 回答
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();
?>
于 2013-06-07T10:58:30.203 回答
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;
}
于 2013-06-07T10:55:10.233 回答