Access indices of an array inside another array

Asked

Viewed 1,922 times

3

I have the array below and need to create a foreachs to go through it and pick up the product and the 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 that way, but he doesn’t recognize the ratings:

foreach($array as $a){
     foreach($a as $produto){
           $p = $produto['produto_id'];
           $q = $produto['qtd'];
     }
}

2 answers

5


I’d mess with your structure a little 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
            )
        )
    )
);

Then the loop would be:

foreach ($array as $loja) {
    foreach ($loja['produtos'] as $produto) {
        $p = $produto['produto'];
        $q = $produto['qtd']; 
    }
}
  • It does not access the indexes: Illegal string offset 'product'.

  • The structure of the second store was different from the first (already in its original code). I corrected in my.

  • https://ideone.com/oaNCvJ

4

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.

Browser other questions tagged

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