Foreach PHP循环数组+字符串

时间:2015-08-26 18:10:54

标签: php arrays simplexml

#1

我该如何合并:

$enderecofoto = $xml->imovel[$i]->fotos->foto;
$nomeeextensao = preg_replace("(^.*\/(.*)\$)", "$1$2",$enderecofoto);
$removenomeeextensao = str_replace($nomeeextensao, "", $enderecofoto);
$nomefoto = substr($nomeeextensao, 0 , (strrpos($nomeeextensao, ".")));
$foto = $xml->imovel[$i]->fotos->addChild('foto');
$foto->addAttribute('path', $removenomeeextensao);
$foto->addAttribute('arquivo', $nomeeextensao);
$foto->addAttribute('titulo', $nomefoto);

#2

进入这个Foreach:

foreach($xml->imovel[$i]->fotos->foto as $foto) { 
    $fotos = array();
    foreach ($xml->imovel[$i]->fotos->foto as $foto) {
      $fotos[] = "$foto"; 
      // or you could use, I believe: $fotos[] = $foto[0] 
    }
  var_dump($fotos); 
  echo "<hr />"; 
}

为了将第一个代码中的单个结果替换为多结果方法。

这应该适用于SimpleXML:D #1我可以获得第一张图片并将其设为xml标签。 #2我可以得到一个包含所有图片的数组。但现在我无法将每个结果转换为simplexml标记。

如何解决?

----- ------ EDIT 使用这个:

foreach($xml->imovel[$i]->fotos->foto as $foto) { 
    $fotos = array();
    foreach ($xml->imovel[$i]->fotos->foto as $foto) {
      $fotos[] = "$foto"; 
      // or you could use, I believe: $fotos[] = $foto[0] 
    }
     var_dump($fotos); 
     echo "<hr />"; 
$nomeeextensao = preg_replace("(^.*\/(.*)\$)", "$1$2",(string)$foto);
$removenomeeextensao = str_replace((string)$nomeeextensao, "", (string)$foto);
$nomefoto = substr((string)$nomeeextensao, 0 , (strrpos((string)$nomeeextensao, ".")));
$fotoxml = $xml->imovel[$i]->fotos->addChild('fotomudar');
$fotoxml->addAttribute('path', (string)$removenomeeextensao);
$fotoxml->addAttribute('arquivo', (string)$nomeeextensao);
$fotoxml->addAttribute('titulo', (string)$nomefoto);
}
unset($xml->imovel[$i]->fotos->foto);

我明白了:

<fotomudar path="http://localhost:8090/xml/" arquivo="IMG_33011.jpg" titulo="IMG_33011"/>
<fotomudar path="http://localhost:8090/xml/" arquivo="IMG_33011.jpg" titulo="IMG_33011"/>
<fotomudar path="http://localhost:8090/xml/" arquivo="IMG_33011.jpg" titulo="IMG_33011"/>
<fotomudar path="http://localhost:8090/xml/" arquivo="IMG_33011.jpg" titulo="IMG_33011"/>
<fotomudar path="http://localhost:8090/xml/" arquivo="IMG_33011.jpg" titulo="IMG_33011"/>
<fotomudar path="http://localhost:8090/xml/" arquivo="IMG_33011.jpg" titulo="IMG_33011"/>
<fotomudar path="http://localhost:8090/xml/" arquivo="IMG_33011.jpg" titulo="IMG_33011"/>
<fotomudar path="http://localhost:8090/xml/" arquivo="IMG_33011.jpg" titulo="IMG_33011"/>
<fotomudar path="http://localhost:8090/xml/" arquivo="IMG_33011.jpg" titulo="IMG_33011"/>
<fotomudar path="http://localhost:8090/xml/" arquivo="IMG_33011.jpg" titulo="IMG_33011"/>
<fotomudar path="http://localhost:8090/xml/" arquivo="IMG_33011.jpg" titulo="IMG_33011"/>
<fotomudar path="http://localhost:8090/xml/" arquivo="IMG_33011.jpg" titulo="IMG_33011"/>
<fotomudar path="http://localhost:8090/xml/" arquivo="IMG_33011.jpg" titulo="IMG_33011"/>

所以...我只得到每个元素重复的第一张图片。 如果有10张照片,我只能用第一张照片重复10行:(

更改时:

$nomeeextensao = preg_replace("(^.*\/(.*)\$)", "$1$2", $foto);
$removenomeeextensao = str_replace($nomeeextensao, "", $foto);

有:

$nomeeextensao = preg_replace("(^.*\/(.*)\$)", "$1$2", $fotos);
$removenomeeextensao = str_replace($nomeeextensao, "", $fotos);

我明白了:

<fotomudar path="Array" arquivo="Array" titulo=""/>
<fotomudar path="Array" arquivo="Array" titulo=""/>
<fotomudar path="Array" arquivo="Array" titulo=""/>
<fotomudar path="Array" arquivo="Array" titulo=""/>
<fotomudar path="Array" arquivo="Array" titulo=""/>
<fotomudar path="Array" arquivo="Array" titulo=""/>
<fotomudar path="Array" arquivo="Array" titulo=""/>
<fotomudar path="Array" arquivo="Array" titulo=""/>
<fotomudar path="Array" arquivo="Array" titulo=""/>
<fotomudar path="Array" arquivo="Array" titulo=""/>
<fotomudar path="Array" arquivo="Array" titulo=""/>
<fotomudar path="Array" arquivo="Array" titulo=""/>

0 个答案:

没有答案
相关问题