致命错误:不能使用stdClass类型的对象作为数组

时间:2012-05-31 09:39:21

标签: php arrays codeigniter

我正在从控制器向模型发送数据 var_dump

$score

如图所示

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] => 
                        )

                )

        )

)

但是当我评估它时

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

抛出错误

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

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

如何解决? 如何避免致命错误,因为它打破了我的进一步处理。 下面一个是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' => '',
      )),
    ),
  ),
)                

2 个答案:

答案 0 :(得分:2)

请勿在{{1​​}}循环中使用相同的var名称($score)两次!

解决方案:

foreach()

答案 1 :(得分:0)

似乎score["assessment_score"][$i]是一个数组 所以要访问1°对象,你需要使用s core["assessment_score"][1][0]