0
All right guys,
I am facing a problem with foreach in PHP, in which I receive an xml and convert it to array.
Only that in some cases xml has only one item (See the 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 inside [material] the foreach returns only a string containing the information:
string '18:00' (length=5)
In other xml with more items inside "material" it traverses correctly because in this case it turns into an array with numeric indices
Look at:
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 ?