Following is my multidimensional array named $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
)
)
)
)
I want to get all the values from key [class_id]
one by one and push the values in an array $data
. But getting the warning
Warning: Invalid argument supplied for foreach()
I'm not understanding what is wrong with my code. Can any one help me to improve my code and append the values to the new array? Thanks in advance. My code is as follows :
$prev_map_data = $objTeacherClassesSubjects->GetClassSubjectMappingsbyTeacherId ($request, $teacher_class_subjects_error_messages);
foreach($prev_map_data as $map_id) {
$cls_data[] = $map_id['class_id'];
}
}
Try this code
$teacher_class_subjects_error_messages);
foreach($prev_map_data as $iKey => $iVal) {
$cls_data[] = $iVal['class_id'];
}
}
$prev_map_data
is not an array. use var_export($prev_map_data);
to see if it is an array
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!';
}