【问题标题】:Trying to access another field when declaring associative array声明关联数组时尝试访问另一个字段
【发布时间】:2018-06-22 07:08:01
【问题描述】:

假设我有这个:

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

那条线抛出undefined,我理解原因,因为$data 它没有完全构建,我无法访问它。那么问题来了,有什么办法可以访问'total'这个字段吗??

【问题讨论】:

    标签: php arrays associative


    【解决方案1】:

    或者先定义

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

    【讨论】:

      【解决方案2】:

      是的,稍后定义。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-09
        • 2014-06-15
        • 2016-12-05
        • 1970-01-01
        • 2021-07-27
        相关资源
        最近更新 更多