2

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'
                    ]
                ]
            ]
        ];
2
  • First of all, shouldn't you be using an if statement in your loop if you are looking to check a condition? Commented Jun 6, 2019 at 5:51
  • 1
    @AlivetoDie thanks for the quick turn out I'm checking the values as I have big array in real will update you Commented Jun 6, 2019 at 6:39

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.