Accessing data from an array

1

I have a half-beast problem here.

I have the following array

array(1) {
  [0]=>
  array(7) {
    ["id"]=>
    string(1) "6"
    ["produto"]=>
    string(1) "7"
    ["peso"]=>
    string(1) "1"
    ["comprimento"]=>
    string(2) "16"
    ["largura"]=>
    string(2) "15"
    ["altura"]=>
    string(1) "5"
    ["diametro"]=>
    string(1) "0"
  }
}

But every time I try to access something from it, php claims undefined index

I'm accessing as follows

$vetor['produto']
$vetor['peso']

What would be the correct way to access it?

    
asked by anonymous 14.02.2016 / 02:50

1 answer

3

It has the index zero before id and the others, the right is $vetor[0]['produto'] . One way to see an organized array of the array or object is to use this code

echo '<pre>';
print_r($arr);

This makes it explicit which element contains which.

The structure of yours is this:

<?php 

$arr = array(
        0 => array('id' => 6, 'produto' => 7, 'peso' => 1, 'comprimento' => 16, 'largura' => 15, 'altura' => 5, 'diametro' => 0)
);

echo '<pre>';
print_r($arr);

And with the <pre> evidence what elements are within the zero index.

Array
(
    [0] => Array
        (
            [id] => 6
            [produto] => 7
            [peso] => 1
            [comprimento] => 16
            [largura] => 15
            [altura] => 5
            [diametro] => 0
        )

)

If you want to zero the index zero you can use array_shift () or array_pop ()

echo '<pre>';
$arr = array_shift($arr);
print_r($arr);

Saida:

Array
(
    [id] => 6
    [produto] => 7
    [peso] => 1
    [comprimento] => 16
    [largura] => 15
    [altura] => 5
    [diametro] => 0
)
    
14.02.2016 / 03:39