将序列号指定为html的ID

I'm sorry for asking similar question again, but I wanted to make my question short and because of this, your solutions doesn't work for me (I thought it will work in similar way in arrays).

I'm getting photos from xml file:

$filelocation=simplexml_load_file('http://path.com/file.xml');
foreach($filelocation->path->attributes() as $photo) {
$position[$flag1]["photos"] .=  "<a id=\"carousel-selector-".$dont-know-what-to-do-here."\"><img src=\"".$photo."\"/></a>";
}

It's of course a part of the code which shows photos placed in xml file. What I need is to place unique id to all of them so it will work like:

<a id="carousel-selector-0"><img src="http://path.com/link.jpg"/></a>
<a id="carousel-selector-1"><img src="http://path.com/another-link.jpg"/></a>
...

Can you help me with that?

You've to maintain the index for each iteration, as we don't know what will be the key, so that we can maintain index outside and will increase the index each time.

$filelocation=simplexml_load_file('http://path.com/file.xml');
$index = 0;
foreach($filelocation->path->attributes() as $photo) {
$position[$flag1]["photos"] .=  "<a id=\"carousel-selector-".$index."\"><img src=\"".$photo."\"/></a>";
$index++;
}

You could use a variable to increment :

$idx = 0;
foreach($filelocation->path->attributes() as $photo) {
  $position[$flag1]["photos"] .=  "<a id=\"carousel-selector-".($idx++)."\"><img src=\"".$photo."\"/></a>";
}

I don't think it needs to be any more complicated than maintaining a simple count of how many times you've looped, like this:

$filelocation=simplexml_load_file('http://path.com/file.xml');
$count = 0; //loop counter

foreach($filelocation->path->attributes() as $photo) {
  $position[$flag1]["photos"] .=  "<a id=\"carousel-selector-".$count."\"><img src=\"".$photo."\"/></a>"; //use the current count
  $count++; //incremenent the count
}