How to mount this multidimensional array via foreach

Asked

Viewed 402 times

2

I’ll just put the part where I’m having trouble!

The array that 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 closest I could come was this! Example 2:

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

        )

)

Let’s go to the real problem, whether I have two or more attributes within the array:

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

When reading from the foreach, so you can add the indexes and values next to the node, "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 it were a group by name + value

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

Complete XML template and already correctly generated with only one attribute!

http://pastebin.com/y28VHAwT

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

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

And it’s only getting the last attribute of the array

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

2 answers

1


Good afternoon,

Depending on how you are generating the xml (Tool you use), the following might 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 result in what is intended, you may experience 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>
  • Ball Show is Library @Masnathan, thanks for sharing, fitted like a glove!

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 be like this:

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

This works, I don’t see much logic in the array you want to build but since I don’t know how the rest of the code is, I know this works. I would set the attributes as index 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 place works.

Regards.

  • Good morning @Wilson, it’s almost that, and about XML, it’s from Buscapé, it’s quite big the code, so I just put the part that is giving this small problem, in its code it generates an index, and this entry generates another error. follow the xml generated http://pastebin.com/y28VHAwT

  • 1

    So the array has to keep this structure right? $xmlArray['attributes']['attribute']['name'] = $name; $xmlArray['attributes']['attribute']['value'] = $value;

  • Yes, I would have some idea, and almost that, but without the indexes! , same as the first example! Thank you.

  • <category>t-shirt</category> <availability>1000</availability> <height>20.5</height> <length>80.5</length> <width>40.5</width> <weight>305</weight> <specifications> Just as there is this in XML there could be no more 2 tags called: <size>35</size> <color>blue</color>

  • I didn’t get the copy!

  • If you look at XML there are several spread attributes like: 'category', 'availability', 'height' etc... Does it also not support attributes like: 'size' and 'color'? I don’t know how Buscap XML works.

  • I followed the model of the Platform, see the link, has just the two attributes and the way it has to be passed.

  • 1

    You’re absolutely right, but then I don’t see a solution. PHP you will not be able to do an array like in the first example because the array’s Dexes are equal in 2.. Only if you send the first part of the array and dps send the other.. I don’t know if I can do this...

  • Okay Wilson Thanks for the help, I’ll see what I do, or if someone comes up with some new balcony, but I think it’s like you said, the Dexes are the same! vlw

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.