Foreach and multidimensional associative array with only one item

Asked

Viewed 182 times

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 ?

1 answer

0

Solving your problem is simple, but since you didn’t post the code, I can only "hint" at what your code does. Anyway, I imagine your code is similar to this:

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

So your code works in the second example. To solve the problem of the first example and continue running evenly, you can make 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 above code does is add to an array when there is only one material. This way, your array will be similar to:

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

Browser other questions tagged

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