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?