Access indices of an array that is inside another array

3

I have the array below and I need to create a foreachs to go through it and get the product and qtd.

$array = array(
            array(
                'loja' => 'Loja1', array(
                    array(
                        'produto' => 'bolsa', 'qtd' => 1
                    ),
                    array(
                        'produto' => 'bolsa 3', 'qtd' => 3
                    )
                )
            ),
            array(
                'loja' => 'Loja2', array(
                    'produto' => 'bolsa 2', 'qtd' => 2
                )
            )
        );

I tried this way, but it does not recognize indexes:

foreach($array as $a){
     foreach($a as $produto){
           $p = $produto['produto_id'];
           $q = $produto['qtd'];
     }
}
    
asked by anonymous 30.01.2018 / 21:08

2 answers

5

I would move your structure a bit, like this:

$array = array(
    array(
        'loja' => 'Loja1', 
        'produtos' => array(
            array(
                'produto' => 'bolsa', 'qtd' => 1
            ),
            array(
                'produto' => 'bolsa 3', 'qtd' => 3
            )
        )
    ),
    array(
        'loja' => 'Loja2', 
        'produtos' => array(
            array(
                'produto' => 'bolsa 2', 'qtd' => 2
            )
        )
    )
);

Here the loop would be:

foreach ($array as $loja) {
    foreach ($loja['produtos'] as $produto) {
        $p = $produto['produto'];
        $q = $produto['qtd']; 
    }
}
    
30.01.2018 / 21:18
4

My friend, this is looking like university course evaluation activity ... but let's see what matters, the foreach structure to go through this DESUNIFORME Array 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 ...

Ideally, you have a uniform structure in this Array to store the data, you will notice that there is a mixture of two-dimensional Arrays with numeric indexes and literal indexes, this is a confusion when you manipulate this data, which you will get very angry when it has many rows and columns ...

But as I know that it is not always possible to do the way we want, if it is possible you change the power structure of this Array I will leave a suggestion, see that 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 bit more standardized and simple.

    
31.01.2018 / 00:37