以下是我的多维数组$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'];
}
}
答案 0 :(得分:0)
试试此代码
$teacher_class_subjects_error_messages);
foreach($prev_map_data as $iKey => $iVal) {
$cls_data[] = $iVal['class_id'];
}
}
答案 1 :(得分:0)
$prev_map_data
不是数组。使用var_export($prev_map_data);
查看它是否为数组
答案 2 :(得分: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!';
}