How do I convert this (which only brings me the first photo)
//corrige foto
$enderecofoto = $xml->imovel[$i]->fotos->foto;
$nomeeextensao = preg_replace("(^.*\/(.*)\$)", "$1$2",$enderecofoto);
$removenomeeextensao = str_replace($nomeeextensao, "", $enderecofoto);
$nomefoto = substr($nomeeextensao, 0 , (strrpos($nomeeextensao, ".")));
//remove antiga tag antes de adicionar a nova, para evitar remoção de ambas
unset($xml->imovel[$i]->fotos->foto);
$foto = $xml->imovel[$i]->fotos->addChild('foto');
$foto->addAttribute('path', $removenomeeextensao);
$foto->addAttribute('arquivo', $nomeeextensao);
$foto->addAttribute('titulo', $nomefoto);
In a loop?
My script is basically this, and this "conversion" would be there:
$xml=simplexml_load_file("feed.xml") or die("Error: Cannot create object");
for ($i = 0; $i < count($xml); $i++) {
@@@@AQUI@@@@
}
echo $xml->asXML();
$xml->asXML('xml.xml');
The Loop should result in:
<fotos>
<foto path="http://localhost:8090/images/" arquivo="1.jpg" titulo="1"/>
<foto path="http://localhost:8090/images/" arquivo="2.jpg" titulo="2"/>
<foto path="http://localhost:8090/images/" arquivo="3.jpg" titulo="3"/>
</fotos>
Instead of (what it is currently):
<fotos>
<foto path="http://localhost:8090/images/" arquivo="1.jpg" titulo="1"/>
</fotos>
What do you say?
Looking at the StackOverflow I found this :
foreach($xml->imovel[$i]->fotos->foto as $foto) {
if(isset($xml->imovel[$i]->fotos->foto)) {
$fotos = array();
foreach ($xml->imovel[$i]->fotos->foto as $foto) {
$fotos[] = "$foto";
// or you could use, I believe: $fotos[] = $foto[0]
}
}
else $fotos = "";
var_dump($fotos);
echo "<hr />";
}
But how do I make this foreach use my settings? For a new XML tag to receive the data as it was set in the config I made up there?
-------------- EDIT
foreach($xml->imovel[$i]->fotos->foto as $foto) {
if(isset($xml->imovel[$i]->fotos->foto)) {
$fotos = array();
foreach ($xml->imovel[$i]->fotos->foto as $foto) {
$fotos[] = "$foto";
$nomeeextensao = preg_replace("(^.*\/(.*)\$)", "$1$2",$foto);
$removenomeeextensao = str_replace($nomeeextensao, "", $foto);
$nomefoto = substr($nomeeextensao, 0 , (strrpos($nomeeextensao, ".")));
$fotoxml = $xml->imovel[$i]->fotos->addChild('foto');
$fotoxml->addAttribute('path', $removenomeeextensao);
$fotoxml->addAttribute('arquivo', $nomeeextensao);
$fotoxml->addAttribute('titulo', $nomefoto);
}
}
else $fotos = "";
var_dump($fotos);
echo "<hr />"
;
}