PHP Zähler: Inkremental-ID für jedes Element in foreach

Habe ich eine foreach-Schleife, ich würde gerne eine inkrementelle id="X" (X ausgehend von 1-zu - sagen wir - 6, wenn 6 Elemente vorhanden sind) für jeden Artikel.

Hier der code:

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

Beispiel: ich möchte, dass id="X" werden hier <a ID="" href="">

InformationsquelleAutor Dio Cane | 2013-06-07
Schreibe einen Kommentar