0

以下是我的多维数组,名为$prev_map_data

Array
(
    [0] => Array
        (
            [class_id] => 2
            [class_name] => II
            [class_checked] => 1
            [class_subjects] => Array
                (
                    [0] => Array
                        (
                            [cs_map_id] => 81
                            [subject_name] => 11 Engllish
                            [subject_checked] => 1
                            [teacher_cs_id] => 81
                        )

                )

        )
[1] => Array
    (
        [class_id] => 3
        [class_name] => III
        [class_checked] => 1
        [class_subjects] => Array
            (
                [0] => Array
                    (
                        [cs_map_id] => 155
                        [subject_name] => Hidi
                        [subject_checked] => 1
                        [teacher_cs_id] => 155
                    )

                [1] => Array
                    (
                        [cs_map_id] => 156
                        [subject_name] => 11 Maths
                        [subject_checked] => 1
                        [teacher_cs_id] => 156
                    )

                [2] => Array
                    (
                        [cs_map_id] => 157
                        [subject_name] => 11 Science
                        [subject_checked] => 1
                        [teacher_cs_id] => 157
                    )

            )

    )

)

我想从键[class_id]中一一获取所有值并将值推送到数组$data中。但收到警告

Warning: Invalid argument supplied for foreach()

我不明白我的代码有什么问题。任何人都可以帮助我改进我的代码并将值附加到新数组中吗?提前致谢。我的代码如下:

 $prev_map_data  = $objTeacherClassesSubjects->GetClassSubjectMappingsbyTeacherId ($request, $teacher_class_subjects_error_messages);
   foreach($prev_map_data as $map_id) {
              $cls_data[] = $map_id['class_id'];
          }
    }
4

3 回答 3

0

试试这个代码

$teacher_class_subjects_error_messages);
   foreach($prev_map_data as $iKey => $iVal) {
              $cls_data[] = $iVal['class_id'];
          }
    }
于 2013-05-08T10:52:41.833 回答
0

$prev_map_data不是数组。用于var_export($prev_map_data);查看是否为数组

于 2013-05-08T11:16:37.137 回答
0
if(!empty($prev_map_data) && is_array($prev_map_data)) {
  foreach($prev_map_data as $key=>$map_id) {
     if(is_array($map_id))
       $cls_data[] = $map_id['class_id'];
     else
       echo '$map_id is not an array!';
  }
} else {
  echo '$prev_map_data is not an array or it is empty!';
}
于 2013-05-09T11:29:41.580 回答