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
                        )

                )

        )

)

[cs_map_id]key からすべての値を1 つずつ取得し、その値を配列にプッシュしたいと考えています$data。しかし、警告を受ける

Warning: Invalid argument supplied for foreach()

コードの何が問題なのか理解できません。コードを改善し、値を新しい配列に追加するのを手伝ってくれる人はいますか? 前もって感謝します。私のコードは次のとおりです。

$prev_map_data  = $objTeacherClassesSubjects->GetClassSubjectMappingsbyTeacherId ($request, $teacher_class_subjects_error_messages);
//print_d($prev_map_data);
$data = array();
foreach($prev_map_data as $map_id){
    foreach($map_id as $ast){
        foreach($ast as $tp){
            //print_p($tp);
            $tp['cs_map_id'];
            array_push($data,$tp['cs_map_id']);
        }
    }
}
4

2 に答える 2

2

それ自体が配列ではないアイテムの配列を無条件に掘り下げています。代わりにこれを行います:

foreach($prev_map_data as $map_id){
    foreach($map_id['class_subjects'] as $subject){
        $data[] = $subject['cs_map_id'];
    }
}
于 2013-05-08T09:08:54.413 に答える
0

これを使って

$arr = array();

foreach($prev_map_data as $map_id){
   $i = 0;
   foreach($map_id['class_subjects'] as $val) {

       $arr[$i] = $val['cs_map_id'];
       $i++;
   }
}
于 2013-05-08T09:08:43.133 に答える