PHP - 关联数组从键一和键二值分配第三个键值

Bis*_*was -1 php arrays associative-array laravel

我有一个关联数组,如下所示:

$sensorThreshold = [
  'ph' => [
    'minPh' => $sensorThreshold->where('name', 'Ph')->last()->min_threshold ?? 0,
    'maxPh' => $sensorThreshold->where('name', 'Ph')->last()->max_threshold ?? 0,
    'sum' =>  $sensorThreshold->where('name', 'Ph')->last()->min_threshold + $sensorThreshold->where('name', 'Ph')->last()->max_threshold,
  ]
]
Run Code Online (Sandbox Code Playgroud)

当我定义数组时如何访问minPh和值?maxPh喜欢:

$sensorThreshold = [
  'ph' => [
    'minPh' => $sensorThreshold->where('name', 'Ph')->last()->min_threshold ?? 0,
    'maxPh' => $sensorThreshold->where('name', 'Ph')->last()->max_threshold ?? 0,
    'sum' =>  minPh + maxPh,
  ]
]
Run Code Online (Sandbox Code Playgroud)

Mic*_*vec 5

您可以将其保存到变量中

$sensorThreshold = [
    'ph' => [
        'minPh' => $minPh = ($sensorThreshold->where('name', 'Ph')->last()->min_threshold ?? 0),
        'maxPh' => $maxPh = ($sensorThreshold->where('name', 'Ph')->last()->max_threshold ?? 0),
        'sum' => $minPh + $maxPh,
    ]
];
Run Code Online (Sandbox Code Playgroud)