0

I am sending data from controller to model var_dump of

$score

it is as shown

Array
(
    [assessment_score] => Array
        (
            [0] => Array
                (
                )

            [1] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 3
                            [score] => 4
                            [assessment_id] => 1
                            [assessor_id] => 24
                            [level_test] => 2
                        )

                    [1] => stdClass Object
                        (
                            [id] => 4
                            [score] => 5
                            [assessment_id] => 1
                            [assessor_id] => 24
                            [level_test] => 3
                        )

                )

            [2] => Array
                (
                )

            [3] => Array
                (
                )

        )

    [assessment_msg] => Array
        (
            [0] => Array
                (
                )

            [1] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 1
                            [user_id] => 21
                            [start_time] => 2012-05-20 20:45:30
                            [status] => 0
                            [end_time] => 2012-05-21 20:45:30
                            [assessor1] => 25
                            [assessor2] => 24
                            [assessor3] => 0
                            [gd_msg] => 
                            [gd_pf] => 
                            [csgd_msg] => 
                            [csgd_pf] => 
                            [pi_msg] => 
                            [pi_pf] => 
                            [wr_msg] => 
                            [wr_pf] => 
                        )

                )

            [2] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 4
                            [user_id] => 28
                            [start_time] => 2012-05-23 13:38:32
                            [status] => 0
                            [end_time] => 2012-05-24 13:38:32
                            [assessor1] => 23
                            [assessor2] => 0
                            [assessor3] => 0
                            [gd_msg] => 
                            [gd_pf] => 
                            [csgd_msg] => 
                            [csgd_pf] => 
                            [pi_msg] => 
                            [pi_pf] => 
                            [wr_msg] => 
                            [wr_pf] => 
                        )

                )

            [3] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 3
                            [user_id] => 22
                            [start_time] => 2012-05-14 14:00:41
                            [status] => 0
                            [end_time] => 2012-05-17 14:00:41
                            [assessor1] => 25
                            [assessor2] => 0
                            [assessor3] => 0
                            [gd_msg] => 
                            [gd_pf] => 
                            [csgd_msg] => 
                            [csgd_pf] => 
                            [pi_msg] => 
                            [pi_pf] => 
                            [wr_msg] => 
                            [wr_pf] => 
                        )

                )

        )

)

but when I am assessing it

$user_score = array();
if (count($score['assessment_score'][$i]) > 0) {
    foreach ($score['assessment_score'][$i] as $score) {
             $user_score[$score->level_test] = $score->score;
    }
}

throws an error

( ! ) Fatal error: Cannot use object of type stdClass as array
in C:\xampp\htdocs\ci\application\views\users\users_score_list.php

to the line

  if (count($score['assessment_score'][$i]) > 0) {

how to solve it? how to avoid fatal error since it breaks my further processing. below one is var_export

array (
  'assessment_score' => 
  array (
    0 => 
    array (
    ),
    1 => 
    array (
      0 => 
      stdClass::__set_state(array(
         'id' => '3',
         'score' => '4',
         'assessment_id' => '1',
         'assessor_id' => '24',
         'level_test' => '2',
      )),
      1 => 
      stdClass::__set_state(array(
         'id' => '4',
         'score' => '5',
         'assessment_id' => '1',
         'assessor_id' => '24',
         'level_test' => '3',
      )),
    ),
    2 => 
    array (
    ),
    3 => 
    array (
    ),
  ),
  'assessment_msg' => 
  array (
    0 => 
    array (
    ),
    1 => 
    array (
      0 => 
      stdClass::__set_state(array(
         'id' => '1',
         'user_id' => '21',
         'start_time' => '2012-05-20 20:45:30',
         'status' => '0',
         'end_time' => '2012-05-21 20:45:30',
         'assessor1' => '25',
         'assessor2' => '24',
         'assessor3' => '0',
         'gd_msg' => '',
         'gd_pf' => '',
         'csgd_msg' => '',
         'csgd_pf' => '',
         'pi_msg' => '',
         'pi_pf' => '',
         'wr_msg' => '',
         'wr_pf' => '',
      )),
    ),
    2 => 
    array (
      0 => 
      stdClass::__set_state(array(
         'id' => '4',
         'user_id' => '28',
         'start_time' => '2012-05-23 13:38:32',
         'status' => '0',
         'end_time' => '2012-05-24 13:38:32',
         'assessor1' => '23',
         'assessor2' => '0',
         'assessor3' => '0',
         'gd_msg' => '',
         'gd_pf' => '',
         'csgd_msg' => '',
         'csgd_pf' => '',
         'pi_msg' => '',
         'pi_pf' => '',
         'wr_msg' => '',
         'wr_pf' => '',
      )),
    ),
    3 => 
    array (
      0 => 
      stdClass::__set_state(array(
         'id' => '3',
         'user_id' => '22',
         'start_time' => '2012-05-14 14:00:41',
         'status' => '0',
         'end_time' => '2012-05-17 14:00:41',
         'assessor1' => '25',
         'assessor2' => '0',
         'assessor3' => '0',
         'gd_msg' => '',
         'gd_pf' => '',
         'csgd_msg' => '',
         'csgd_pf' => '',
         'pi_msg' => '',
         'pi_pf' => '',
         'wr_msg' => '',
         'wr_pf' => '',
      )),
    ),
  ),
)                
3
  • What do you need the outer if for? Commented May 31, 2012 at 9:45
  • I want to avoid fatal error since it stops my further execution Commented May 31, 2012 at 9:46
  • @Poonam show a var_export instead of var_dump; somehow we're missing something. Commented May 31, 2012 at 9:47

2 Answers 2

2

Don't use the same var name ($score) twice in the foreach() loop!

Solution:

if (count($score['assessment_score'][$i]) > 0) {
    foreach ($score['assessment_score'][$i] as $data) { // $data instead of $score
        $user_score[$data->level_test] = $data->score;  // $data instead of $score
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

its ok I understood what you were trying to say, thanks for your help @powtac
0

seems that score["assessment_score"][$i] is an array so to access the 1° object u need to use score["assessment_score"][1][0]

1 Comment

thats not an issue, issue with [assessment_score][2] , since it is empty, even I don't know how it works for [assessment_score][0]

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.