Search code examples
phparrayssimplexml

Foreach PHP Loop Array + String


#1

How can I merge this:

$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

Into this 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 />"; 
}

In order to replace single result from the first code into a multiple results method.

This is supposed to work with SimpleXML :D The #1 I can get the first picture and make it a xml tag. The #2 I could get an array with ALL pictures. But now I'm unable to transform each result into simplexml tag.

How can it be fixed?

-----EDIT------ Using this:

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

I get:

<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"/>

So... I get only the first picture of each element duplicated. If there is 10 photos, I get 10 repeated lines with first picture only :(

When changing:

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

for:

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

I get:

<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=""/>

Solution

  • I did it!

    foreach($xml->imovel[$i]->fotos->foto as $foto) { 
        $fotos = array();
        $nomeeextensao = preg_replace("(^.*\/(.*)\$)", "$1$2",$foto);
        $removenomeeextensao = str_replace($nomeeextensao, "",$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);
    

    Now just save via simplexml and open via file_get_content and use str_replace to change "fotomudar" for "foto". :D