Reputation: 337
I'm having an array where I need to compare there value and create a new array
I need to compare marks_obt and passing_marks. if marks_obt > passing_marks
then change style element inside array
I tried using foreach but not able to create expected output
foreach ($data as $key => $value) {
$finalout['data'] = $value[0];
for ($i=0; $i < count($value['score']) ; $i++) {
$newarray['data'][] = $value['score'][$i];
}
}
this is the input array where i need to compare marks_obt and passing_marks. if marks_obt > passing_marks then add one style element in
$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
)
)
)
and expected output is
$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'
]
]
]
];
Upvotes: 2
Views: 748
Reputation: 72299
Based on your condition statement as well by looking your output structure do like below:
$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);
Output:-https://3v4l.org/p2aGD
Upvotes: 1