我想从以下数组中创建条件,但它没有给出我的预期结果,因为它没有运行第二个条件。
$arr = [
[
472 => [
'EL' => 52.9,
'MT' => 57.375,
'MO' => 56.6,
'SC' => 26,
'ET' => 50.775
]
], [
505 => [
'EL' => 53.425,
'MT' => 25,
'MO' => 62.8,
'SC' => 23,
'ET' => 25
]
]
];
$total = array_reduce(
$arr,
function($arr, $key) {
$id = key($key);
$consumed = $key[$id];
$sc = array_keys($consumed);
$arr[$id] = [
"totalc" => array_sum($consumed),
"condition" => array_search('SC', $sc) ? min($consumed) >= 23:26
];
return $arr;
},
[]
);
print_r($total) 输出以下内容:
Array
(
[472] => Array
(
[totalc] => 243.65
[condition] => 1
)
[505] => Array
(
[totalc] => 189.225
[condition] => 1
)
)
我的预期输出如下:
Array
(
[472] => Array
(
[totalc] => 243.65
[condition] => 1
)
[505] => Array
(
[totalc] => 189.225
[condition] =>
)
)
您可以清楚地看到我要检查:当它是'SC'时,我希望最小值为23。但对于其他人,我希望最小值为26。所以我使用array_search()扫描数组时元素是“SC”。不幸的是,它只检查“SC”而不检查其他。请帮我。