如何在多维数组中插入新的键和值? [英] How to insert a new key and value in multidimensional array?

查看:70
本文介绍了如何在多维数组中插入新的键和值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的多维数组$csmap_data

Array
(
    [0] => Array
        (
            [cs_map_id] => 84
            [cs_subject_id] => 1
        )

    [1] => Array
        (
            [cs_map_id] => 85
            [cs_subject_id] => 5
        )

    [flag] => 1
)

最初,数组中没有[flag] => 1键值,我将其添加到数组$csmap_data中. 但是我想在上述两个数组元素中添加[flag] => 1,而不是作为单独的数组元素.简而言之,我想要以下输出:

Initially there was no [flag] => 1 key-value in the array, I added it to the array $csmap_data. But I want to add the [flag] => 1 in the above two array elements, not as a separate array element. In short I wanted following output :

Array
    (
        [0] => Array
            (
                [cs_map_id] => 84
                [cs_subject_id] => 1
                [flag] => 1
            )

        [1] => Array
            (
                [cs_map_id] => 85
                [cs_subject_id] => 5
                [flag] => 1
            )
       )

我试图实现的代码如下,但无法获得所需的输出:

The code I was trying to achieve this is as follows, but couldn't get the desired output:

if (!empty($csmap_data)) {  
                    foreach($csmap_data as $csm) {
                        $chapter_csmap_details = $objClassSubjects->IsClassSubjectHasChapters($csm['cs_map_id']);

                            $csmap_data ['flag'] = 1;


                    }
            }

有人可以帮助我获得我所描述的所需输出吗?提前致谢.

Can anyone help me out in obtaining the desired output as I depicted? Thanks in advance.

推荐答案

<?
 foreach($csmap_data as $key => $csm)
 {
  $csmap_data[$key]['flag'] = 1;
 }

应该可以解决问题.

这篇关于如何在多维数组中插入新的键和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆