Buddy, this is looking like an evaluative college course activity... but let’s get down to business, the structure foreach
to traverse this Array UNEVEN yours is:
foreach ( $array as $loja ) {
echo "<strong>Loja: {$loja['loja']} </strong><br>";
$itens = $loja[0];
if ( isset( $itens['produto'] ) ) {
echo "-Item: {$itens['produto']} <br>";
echo "-Qtd: {$itens['qtd']} <br><br>";
} else {
foreach ( $loja[0] as $key => $value ) {
echo "-Item: {$value['produto']} <br>";
echo "-Qtd: {$value['qtd']} <br><br>";
}
}
}
Note: I took the liberty of including some HTML tags for better display...
The ideal is that you have a uniform structure in this Array to store the data, it is perceived that there is a mixture of two-dimensional Arrays with numeric indices and literal indices, that gives a confusion when you manipulate this data, that you will go through a lot of anger when it has many rows and columns...
But as I know that it is not always possible to do as we wish, if it is possible to change the power structure of this Array I will leave a suggestion, see that the reading is simpler too.
$array = array(
array(
'loja' => 'Loja1',
'itens' => array(
array(
'produto' => 'bolsa', 'qtd' => 1
),
array(
'produto' => 'bolsa 3', 'qtd' => 3
)
)
),
array(
'loja' => 'Loja2',
'itens' => array(
array(
'produto' => 'bolsa 2', 'qtd' => 2
)
)
)
);
foreach ( $array as $loja ) {
echo "<strong>Loja: {$loja['loja']} </strong><br>";
foreach ( $loja['itens'] as $itens ) {
echo "-Item: {$itens['produto']} <br>";
echo "-Qtd: {$itens['qtd']} <br><br>";
}
}
In this format, the hierarchy becomes a little more standardized and simple.
It does not access the indexes: Illegal string offset 'product'.
– Diego Vieira
The structure of the second store was different from the first (already in its original code). I corrected in my.
– bfavaretto
https://ideone.com/oaNCvJ
– bfavaretto