我有一个数组,需要在此处比较值并创建一个新数组
我需要比较marks_obt和passing_marks。如果marks_obt > passing_marks
然后在数组中更改样式元素
我尝试使用foreach,但无法创建预期的输出
foreach ($data as $key => $value) {
$finalout['data'] = $value[0];
for ($i=0; $i < count($value['score']) ; $i++) {
$newarray['data'][] = $value['score'][$i];
}
}
这是我需要比较marks_obt和passing_marks的输入数组。如果marks_obt>通过标记,则在其中添加一个样式元素
$data = Array
(
[0] => Array
(
[0] => 'Max tide'
['marks_obt'] => Array
(
[0] => 2.00
[1] => 5.00
)
[passing_marks] => Array
(
[0] => 3.00
[1] => 3.00
)
)
[1] => Array
(
[0] => David pixal
[marks_obt] => Array
(
[0] => 5.00
[1] => 5.00
)
[passing_marks] => Array
(
[0] => 3.00
[1] => 3.00
)
)
)
,预期输出为
$finalout = [
[
'data' => [
[
'data' => 'Max tide',
'style' => 'background-color: red; text-align: center'
],
[
'data' => 2,
'style' => 'background-color: red; text-align: center'
],
[
'data' => 5,
'style' => 'background-color: pink; text-align: center'
]
]
],
[
'data' => [
[
'data' => 'David pixal',
],
[
'data' => 5.00,
'style' => 'background-color: pink; text-align: center'
],
[
'data' => 5.00,
'style' => 'background-color: pink; text-align: center'
]
]
]
];
答案 0 :(得分:1)
根据您的条件声明,通过查看输出结构,如下所示:
$finalOutput = array();
foreach ($data as $key => $value) {
$innerArray = array();
$innerArray[] = array('data'=>$value[0],'style'=> 'background-color: red; text-align: center');
foreach($value['marks_obt'] as $k=>$v){
if( isset($value['passing_marks'][$k]) && $v > $value['passing_marks'][$k] ){
$innerArray[] = array('data'=>$v,'style'=> 'background-color: pink; text-align: center');
}else{
$innerArray[] = array('data'=>$v,'style'=> 'background-color: red; text-align: center');
}
}
$finalOutput[] = array('data'=> $innerArray);
}
print_r($finalOutput);