dpir3950 2013-05-07 06:35
浏览 31
已采纳

如何在PHP中的关联数组中插入新的键值对? [关闭]

I've an associative array named $classes_data as follows:

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
                )

        )

)

The keys of array (viz. 2,3,4,6,7,8,9) are in this manner instead of 0,1,2,3,4,5,6 because I've used one function to rearrange these keys.

Now what I want to do is insert a new key class_checked and set its initial value as 0 (i.e.class_checked =>"0").

I tried lot of tricks but couldn't get the desired array format. Can any one help me in this to get the desired array? Thanks in advance.

For your information the required array format for array $classes_data will be as follows:

 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
                )

        )

)
  • 写回答

3条回答 默认 最新

  • doukang7486 2013-05-07 06:44
    关注

    Try:

    foreach($classes_data as $key=>$value) {
        $classes_data[$key]['class_checked'] = 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?