Search code examples
phparraysassociative

Trying to access another field when declaring associative array


Let's say I have this:

$data = [
'total' => $itemNull ? 0 : $issueData['workedHours'] * $itemFound->sale_price,
'tax' => $taxRate * $data['total'], 
];

That line throws undefined and I understand the reason, since $data it is not fully build I cannot access it. So here is the question, is there any way to access the 'total' field??


Solution

  • Or define it first

    $total = $itemNull ? 0 : $issueData['workedHours'] *$itemFound->sale_price;
    
    $data = [
    'total' => $total,
    'tax' => $taxRate * $total, 
    ];