Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将序列号指定为html';s ID_Php - Fatal编程技术网

Php 将序列号指定为html';s ID

Php 将序列号指定为html';s ID,php,Php,很抱歉再次提出类似的问题,但我想把我的问题简短一点,因此,您的解决方案对我不起作用(我认为它在数组中也会以类似的方式起作用) 我正在从xml文件获取照片: $filelocation=simplexml_load_file('http://path.com/file.xml'); foreach($filelocation->path->attributes() as $photo) { $position[$flag1]["photos"] .= "<a id=\"caro

很抱歉再次提出类似的问题,但我想把我的问题简短一点,因此,您的解决方案对我不起作用(我认为它在数组中也会以类似的方式起作用)

我正在从xml文件获取照片:

$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>";
}
$filelocation=simplexml\u load\u文件('http://path.com/file.xml');
foreach($filelocation->path->attributes()作为$photo){
$position[$flag1][“照片”]。=“”;
}
当然,这是显示放在xml文件中的照片的代码的一部分。我需要的是将唯一的id放置到所有这些文件中,这样它的工作方式如下:

<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>
...

...

你能帮我吗?

你可以用一个变量来增加:

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

您可以使用变量来增加:

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

我认为它不需要比简单地计算循环次数更复杂,比如:

$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
}
$filelocation=simplexml\u load\u文件('http://path.com/file.xml');
$count=0//循环计数器
foreach($filelocation->path->attributes()作为$photo){
$position[$flag1][“photos”]。=“”;//使用当前计数
$count++;//递增计数
}

我认为它不需要比简单地计算循环次数更复杂,比如:

$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
}
$filelocation=simplexml\u load\u文件('http://path.com/file.xml');
$count=0//循环计数器
foreach($filelocation->path->attributes()作为$photo){
$position[$flag1][“photos”]。=“”;//使用当前计数
$count++;//递增计数
}

您必须为每次迭代维护索引,因为我们不知道键是什么,所以我们可以在外部维护索引,并每次增加索引

$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++;
}
$filelocation=simplexml\u load\u文件('http://path.com/file.xml');
$index=0;
foreach($filelocation->path->attributes()作为$photo){
$position[$flag1][“照片”]。=“”;
$index++;
}

您必须为每次迭代维护索引,因为我们不知道键是什么,所以我们可以在外部维护索引,并每次增加索引

$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++;
}
$filelocation=simplexml\u load\u文件('http://path.com/file.xml');
$index=0;
foreach($filelocation->path->attributes()作为$photo){
$position[$flag1][“照片”]。=“”;
$index++;
}

我们可以使用
foreach($filelocation->path->attributes()作为$key=>$photo){$position[$flag1][“photos”]。=”;$index++;}
注意:在这种情况下,$key应该被索引为i,e数值。谢谢,这确实有效,但稍后,我会按照产品的价格和ID对照片进行排序:(我们可以通过使用
foreach($filelocation->path->attributes()as$key=>$photo){$position[$flag1][“photos”]。=”;$index++;}
注意:在这种情况下,$key应该被索引为i,e数值。谢谢,这实际上是可行的,但稍后,我会按照产品价格和ID对照片进行排序:(谢谢,您的回答也很有帮助,但在循环之后,我将按产品价格和ID对照片进行排序:(如果它有用的话,你也可以向上投票,即使你不接受它作为主要答案。顺便说一句,因为我们不知道你的代码在循环之后做了什么,所以很难提供任何帮助。也许你应该问一个新的问题。谢谢,你的答案也很有用,但是在循环之后,我会按产品价格对照片进行排序e和ID是混合的:(如果它有用的话,你也可以向上投票,即使你不接受它作为主要答案。顺便说一句,因为我们不知道你的代码在循环之后做了什么,所以很难提供任何帮助。也许你应该问一个新的问题。