在数组中,如果 POAIh_sum 值不存在,则在数组中传递零 这是输入数组。
Array
(
[0] => Array
(
[POAIh_sum] => 417.225
[id] => 2815960
[ts] => 2024-01-01T00:00:00+05:30
)
[1] => Array
(
[POAIh_sum] => 941.942
[id] => 2815960
[ts] => 2024-01-02T00:00:00+05:30
)
)
我已经尝试了下面的代码,但仍然没有得到正确的解决方案
if (array_key_exists('POAIh_sum', $array_value)) {
$POAIh_sum_monthlyreport[] = $array_value['POAIh_sum'];
} else {
$POAIh_sum_monthlyreport[] =0;
}
我需要如下输出。该数组基于 POAIh_sum 值
Array
(
[0] => Array
(
[POAIh_sum] => 0
[id] => 2815961
[ts] => 2024-01-01T00:00:00+05:30
)
[1] => Array
(
[POAIh_sum] => 0
[id] => 2815961
[ts] => 2024-01-02T00:00:00+05:30
)
)
$input_array = array(
array(
'POAIh_sum' => 417.225,
'id' => 2815960,
'ts' => '2024-01-01T00:00:00+05:30'
),
array(
'POAIh_sum' => 941.942,
'id' => 2815960,
'ts' => '2024-01-02T00:00:00+05:30'
)
);
$output_array = array();
foreach ($input_array as $array_value) {
if (array_key_exists('POAIh_sum', $array_value)) {
$output_array[] = $array_value;
} else {
$output_array[] = array(
'POAIh_sum' => 0,
'id' => $array_value['id'],
'ts' => $array_value['ts']
);
}
}
print_r($output_array);