Foreach and multidimensional associative array with only one item

0

All good personal,

I'm facing a problem with foreach in PHP, where I get an xml and convert it to array.

In some cases xml has only one item (see example below)

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<blocos>
<versao>1.0</versao>
<material>
<bloco>18:00</bloco>
<sequencia>1</sequencia>
<codigo>00031</codigo>
</material>
</blocos>

Converted to array it looks like this:

Array ( [versao] => 1.0 [material] => Array ( [bloco] => 18:00 [sequencia] => 1 [codigo] => 00031 ) ) 

When I try to foreach the items within [material] foreach returns only a string containing the information:

  

string '18: 00 '(length = 5)

In other xml with more items inside "material" it goes through correctly because in this case it becomes an array with numeric indexes

See:

Array ( [versao] => 1.0 [material] => Array ( [0] => Array ( [bloco] => 07:30 [sequencia] => 1 [codigo] => 00552 ) [1] => Array ( [bloco] => 07:45 [sequencia] => 1 [codigo] => 00042 ) [2] => Array ( [bloco] => 09:45 [sequencia] => 1 [codigo] => 00552 ) [3] => Array ( [bloco] => 13:15 [sequencia] => 1 [codigo] => 00552 ) [4] => Array ( [bloco] => 13:30 [sequencia] => 1 [codigo] => 00042 ) [5] => Array ( [bloco] => 16:00 [sequencia] => 1 [codigo] => 00014 ) [6] => Array ( [bloco] => 16:30 [sequencia] => 1 [codigo] => 00552 ) [7] => Array ( [bloco] => 18:00 [sequencia] => 1 [codigo] => 00031 ) [8] => Array ( [bloco] => 20:45 [sequencia] => 1 [codigo] => 00552 ) [9] => Array ( [bloco] => 21:15 [sequencia] => 1 [codigo] => 00042 ) ) )


<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<blocos>
<versao>1.0</versao>
<material>
<bloco>07:00</bloco>
<sequencia>1</sequencia>
<codigo>00042</codigo>
</material>
<material>
<bloco>13:45</bloco>
<sequencia>1</sequencia>
<codigo>00042</codigo>
</material>
<material>
<bloco>16:00</bloco>
<sequencia>1</sequencia>
<codigo>00014</codigo>
</material>
<material>
<bloco>18:00</bloco>
<sequencia>1</sequencia>
<codigo>00031</codigo>
</material>
</blocos>

How to get around this?

    
asked by anonymous 29.11.2016 / 15:57

1 answer

0

Solving your problem is simple, but since you have not posted the code, I can only "give a tip" by imagining what your code does. Anyway, I imagine your code looks like this:

foreach($xml['material'] as $material)
{
    /** código para tratar o material **/
}

In this way, your code works in the second example. To solve the problem of the first example and continue to run evenly, you can leave the first "array equal" to the second. That is, the two are uniform.

$materialArray = isset($xml['material']['bloco']) ? [$xml['material']] : $xml['material'];

foreach($materialArray as $material)
{
    /** código para tratar o material **/
}

Basically what the code above does is add to an array when there is only one material. That way, your array will be similar to:

Array ( [versao] => 1.0 [material] => Array ( [0] => Array ( [bloco] => 18:00 [sequencia] => 1 [codigo] => 00031 )) )
    
30.11.2016 / 14:01