Referencing an array value within itself

0

I have a function inside a class, and within that function I have the following vector:

$compra = [

    "produto" => "Chocolate Nestle",
    "preco"   => 5.5,
    "frete"   => 1.5,
    "total"   => $compra['preco'] + $compra['frete']

];

I need to make the sum of the price and freight within the vector itself, I use var_dump to show me the result and this error happens:

  

var_dump ($ purchase);

NOTICE Undefined variable: compra on line number 10

NOTICE Undefined variable: compra on line number 10
array(4) { ["produto"]=> string(16) "Chocolate Nestle" ["preco"]=> float(5.5) ["frete"]=> float(1.5) ["total"]=> int(0) }

line 10 is the line that makes the sum

    
asked by anonymous 11.10.2018 / 21:08

1 answer

0

Could you please try the following code?

$compra = [

    "produto" => "Chocolate Nestle",
    "preco"   => 5.5,
    "frete"   => 1.5,
    "total"   => $compra.preco + $compra.frete
];

Or else:

$compra = [

    "produto" => "Chocolate Nestle",
    "preco"   => 5.5,
    "frete"   => 1.5,
    "total"   => ["preco"] + ["frete"]
];
    
12.10.2018 / 01:07