How to mount this multidimensional array via foreach

2

I'll only put the part where I'm having problems!

The% w / w I need, needs to be in this format. Example 1:

Array
(
    [atributos] => Array
        (
            [atributo] => Array
                (
                    [nome] => Tamanho
                    [valor] => 35
                ),

            [atributo] => Array
                (
                    [nome] => Cor
                    [valor] => AZUL/ROSA
                )

        )

)

But the most I got close to was this! Example 2:

Array
(
    [atributos] => Array
        (
            [atributo] => Array
                (
                    [nome] => Cor
                    [valor] => AZUL/ROSA
                )

        )

)

Let's face the real problem if I have two or more attributes inside the array:

$arrayAtributos = array('Tamanho' => 35, 'Cor' => 'AZUL/ROSA');

When reading the% com_of%, so you can add the indexes and values next to the attributes / attribute

$arrayAtributos = array('Tamanho' => 35, 'Cor' => 'AZUL/ROSA');

foreach ($arrayAtributos as $nome => $valor) {

     $xmlArray['atributos']['atributo']['nome'] = $nome;
     $xmlArray['atributos']['atributo']['valor'] = $valor;

}

print_r($xmlArray);

Only the last index is added to the array, ignoring the rest, as if giving a group by name + value

How do you do it, so that all attributes are added within this multidimensional array using array , as in the first example?

Full XML template and already being generated correctly with only one attribute!

link

But I need to add more attributes as in the example:

<atributos>
      <atributo>
           <nome>Tamanho</nome>
           <valor>35</valor>
      </atributo>
      <atributo>
           <nome>Cor</nome>
           <valor>AZUL/ROSA</valor>
      </atributo>
</atributos>

And is getting only with the last array attribute

<atributos>      
     <atributo>
           <nome>Cor</nome>
           <valor>AZUL/ROSA</valor>
     </atributo>
</atributos>
    
asked by anonymous 25.08.2015 / 08:04

2 answers

1

Good afternoon,

Depending on how you are generating the xml (Tool you use), the following may work:

$arrayAtributos = array('Tamanho' => 35, 'Cor' => 'AZUL/ROSA');

foreach ($arrayAtributos as $nome => $valor) {
    $detalhes = array(
        'nome' => $nome,
        'valor' => $valor,
    );

    $xmlArray['atributos']['atributo'][] = $detalhes;
}

If this does not work for you, you can try using this library :

$ composer require masnathan/parser

The encode of the array would be done as follows:

Parser::data($xmlArray)->setPrettyOutput(true)->to('xml');

I just experimented with the data you put here:

<?xml version="1.0" encoding="utf-8"?>
<atributos>
  <atributo>
    <nome>Tamanho</nome>
    <valor>35</valor>
  </atributo>
  <atributo>
    <nome>Cor</nome>
    <valor>AZUL/ROSA</valor>
  </atributo>
</atributos>
    
25.08.2015 / 19:13
1

Good morning, Joker, Yes the problem is in this section

foreach ($arrayAtributos as $nome => $valor) {

     $xmlArray['atributos']['atributo']['nome'] = $nome;
     $xmlArray['atributos']['atributo']['valor'] = $valor;

}

It should look like this:

$x = 0;
foreach ($arrayAtributos as $nome=> $valor) {
        $xmlArray['atributos'][$x]['nome'] = $nome;
        $xmlArray['atributos'][$x]['valor'] = $valor;
        $x++;
}

This results, I do not see great logic in the array you want to build but as I do not know how the rest of the code is, I know this works. I would put the attributes as indexes of the new array.

Example:

Array
(
    [atributos] => Array
        (
            [tamanho] => 35,
            [cor] => AZUL/ROSA
        )

)

This makes more sense to me. But the solution I put in works.

Regards.

    
25.08.2015 / 09:40