0

$classes_data次のような名前の連想配列があります。

Array
(
    [2] => Array
        (
            [class_id] => 2
            [class_name] => II
            [subjects] => Array
                (
                    [0] => 11 Engllish
                )

        )

    [3] => Array
        (
            [class_id] => 3
            [class_name] => III
            [subjects] => Array
                (
                    [0] => Hidi
                    [1] => 11 Maths
                    [2] => 11 Science
                    [3] => 11 Engllish
                )

        )

    [4] => Array
        (
            [class_id] => 4
            [class_name] => IV
            [subjects] => Array
                (
                    [0] => Physics
                )

        )

    [6] => Array
        (
            [class_id] => 6
            [class_name] => VI
            [subjects] => Array
                (
                    [0] => Mathematics
                    [1] => dfadadadsagfasrsarasrarBiology
                )

        )

    [7] => Array
        (
            [class_id] => 7
            [class_name] => VII
            [subjects] => Array
                (
                    [0] => Physics
                    [1] => Chemistry11
                    [2] => 11 Science
                )

        )

    [8] => Array
        (
            [class_id] => 8
            [class_name] => VIII
            [subjects] => Array
                (
                    [0] => Hidi
                    [1] => 11 Engllish
                )

        )

    [9] => Array
        (
            [class_id] => 9
            [class_name] => IX
            [subjects] => Array
                (
                    [0] => Mathematics
                    [1] => Hidi
                    [2] => 11 Science
                )

        )

)

配列のキー (つまり、2,3,4,6,7,8,9) は、0,1,2,3,4,5,6 の代わりにこのようになっています。これは、1 つの関数を使用してこれらを再配置したためです。キー。

今私がやりたいことは、新しいキーclass_checkedを挿入し、その初期値を0(つまりclass_checked =>"0") に設定することです。

多くのトリックを試しましたが、目的の配列形式を取得できませんでした。これで目的の配列を取得するのを手伝ってくれる人はいますか? 前もって感謝します。

参考までに、array に必要な配列形式は$classes_data次のとおりです。

 Array
    (
    [2] => Array
        (
            [class_id] => 2
            [class_name] => II
            [class_checked] => 0
            [subjects] => Array
                (
                    [0] => 11 Engllish
                )

        )

    [3] => Array
        (
            [class_id] => 3
            [class_name] => III
            [class_checked] => 0
            [subjects] => Array
                (
                    [0] => Hidi
                    [1] => 11 Maths
                    [2] => 11 Science
                    [3] => 11 Engllish
                )

        )

    [4] => Array
        (
            [class_id] => 4
            [class_name] => IV
            [class_checked] => 0
            [subjects] => Array
                (
                    [0] => Physics
                )

        )

    [6] => Array
        (
            [class_id] => 6
            [class_name] => VI
            [class_checked] => 0
            [subjects] => Array
                (
                    [0] => Mathematics
                    [1] => dfadadadsagfasrsarasrarBiology
                )

        )

    [7] => Array
        (
            [class_id] => 7
            [class_name] => VII
            [class_checked] => 0
            [subjects] => Array
                (
                    [0] => Physics
                    [1] => Chemistry11
                    [2] => 11 Science
                )

        )

    [8] => Array
        (
            [class_id] => 8
            [class_name] => VIII
            [class_checked] => 0
            [subjects] => Array
                (
                    [0] => Hidi
                    [1] => 11 Engllish
                )

        )

    [9] => Array
        (
            [class_id] => 9
            [class_name] => IX
            [class_checked] => 0
            [subjects] => Array
                (
                    [0] => Mathematics
                    [1] => Hidi
                    [2] => 11 Science
                )

        )

)
4

3 に答える 3

4

試す:

foreach($classes_data as $key=>$value) {
    $classes_data[$key]['class_checked'] = 0;
}
于 2013-05-07T06:44:00.653 に答える
4
$array = array_map(function (array $i) { return $i + array('class_checked' => 0); }, $array);

または:

foreach ($array as &$i) {
    $i['class_checked'] = 0;
}
unset($i);
于 2013-05-07T06:38:48.437 に答える
0

あなたのイニシャルを考えると、$classes_dataこれを行うことができます:

foreach($classes_data as &$class) {
  $class['class_checked'] = 0;
}

これで、配列は希望どおりになるはずです。

于 2013-05-07T06:39:21.717 に答える