0

我正在将数据从控制器发送到模型 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' => '',
      )),
    ),
  ),
)                
4

2 回答 2

2

不要在循环中使用相同的 var name ( $score) 两次!foreach()

解决方案:

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
    }
}
于 2012-05-31T09:51:47.073 回答
0

似乎这score["assessment_score"][$i]是一个数组,因此要访问 1° 对象,您需要使用 score["assessment_score"][1][0]

于 2012-05-31T09:48:43.287 回答