dongrong1856 2013-11-12 16:08
浏览 92
已采纳

如何在PHP中的现有数组中插入新元素?

I'm having an array named $topic_notes as follows:

Array
(
    [0] => Array
        (
            [topic_id] => 214
            [topic_subject_id] => 4
            [topic_notes] => Nice Story
        )

    [1] => Array
        (
            [topic_id] => 215
            [topic_subject_id] => 4
            [topic_notes] => 
        )

    [2] => Array
        (
            [topic_id] => 216
            [topic_subject_id] => 4
            [topic_notes] => 
        )

    [3] => Array
        (
            [topic_id] => 217
            [topic_subject_id] => 4
            [topic_notes] => 
        )

    [4] => Array
        (
            [topic_id] => 218
            [topic_subject_id] => 4
            [topic_notes] => 
        )

    [5] => Array
        (
            [topic_id] => 219
            [topic_subject_id] => 4
            [topic_notes] => 
        )

    [6] => Array
        (
            [topic_id] => 220
            [topic_subject_id] => 4
            [topic_notes] => 
        )

    [7] => Array
        (
            [topic_id] => 221
            [topic_subject_id] => 4
            [topic_notes] => 
        )

    [8] => Array
        (
            [topic_id] => 223
            [topic_subject_id] => 4
            [topic_notes] => 
        )

    [9] => Array
        (
            [topic_id] => 504
            [topic_subject_id] => 4
            [topic_notes] => 
        )

    [10] => Array
        (
            [topic_id] => 225
            [topic_subject_id] => 4
            [topic_notes] => 
        )

)

Now I want to make a new key value pair in each internal array present within array $topic_notes but I'm not able to do. The code which I've tried is as follows:

foreach ($topic_notes as $topic)
  {
    $sql  = "SELECT subject_name FROM ".TBL_SUBJECTS." WHERE subject_id=".$topic['topic_subject_id'];
    $gDb->Query($sql);
    $topic_subject = $gDb->FetchArray(MYSQL_FETCH_SINGLE);
    $subject_name  = $topic_subject['subject_name'];

    $topic_notes['subject'] = $subject_name;     
  }

After executing this code I'm getting following output:

Array
    (
        [0] => Array
            (
                [topic_id] => 214
                [topic_subject_id] => 4
                [topic_notes] => Nice Story
            )

        [1] => Array
            (
                [topic_id] => 215
                [topic_subject_id] => 4
                [topic_notes] => 
            )

        [2] => Array
            (
                [topic_id] => 216
                [topic_subject_id] => 4
                [topic_notes] => 
            )

        [3] => Array
            (
                [topic_id] => 217
                [topic_subject_id] => 4
                [topic_notes] => 
            )

        [4] => Array
            (
                [topic_id] => 218
                [topic_subject_id] => 4
                [topic_notes] => 
            )

        [5] => Array
            (
                [topic_id] => 219
                [topic_subject_id] => 4
                [topic_notes] => 
            )

        [6] => Array
            (
                [topic_id] => 220
                [topic_subject_id] => 4
                [topic_notes] => 
            )

        [7] => Array
            (
                [topic_id] => 221
                [topic_subject_id] => 4
                [topic_notes] => 
            )

        [8] => Array
            (
                [topic_id] => 223
                [topic_subject_id] => 4
                [topic_notes] => 
            )

        [9] => Array
            (
                [topic_id] => 504
                [topic_subject_id] => 4
                [topic_notes] => 
            )

        [10] => Array
            (
                [topic_id] => 225
                [topic_subject_id] => 4
                [topic_notes] => 
            )
[subject] => 12 PHYSICS

    )

The new key-value pair(i.e. new array element) is getting at last position. Actually I want this element within each(all the 10 elements) array element.

  • 写回答

4条回答 默认 最新

  • dongtun4268 2013-11-12 16:16
    关注

    You would be better just getting this all from the DB in the first query (JOIN), but in case that is not possible, use a reference and then change that:

    foreach ($topic_notes as &$topic)
      {
        $sql  = "SELECT subject_name FROM ".TBL_SUBJECTS." WHERE subject_id=".$topic['topic_subject_id'];
        $gDb->Query($sql);
        $topic_subject = $gDb->FetchArray(MYSQL_FETCH_SINGLE);
    
        $topic['subject'] = $topic_subject['subject_name'];     
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 MIT控制器能控制不稳定系统吗
  • ¥15 公司代码X对业务伙伴X无效,处理方法?
  • ¥15 微信内链接跳转到浏览器打开怎么实现
  • ¥15 三角波可以直接加施密特电路整形到矩形波吗实物
  • ¥15 html,php,在使用html请求php文件时发生了错误,无法请求到php文件读取数据库并用javascript进行数据显示,刷新
  • ¥15 touchsocket udp组播
  • ¥20 MAC怎么安装Silverlight 插件?以及安装了怎么启用
  • ¥15 VS2012中查询语句无法填入解析,数值传不进去
  • ¥15 gis系统开发出现命名空间“ESRI.ArcGIS”中不存在类型或命名空间名“Analyst3D”报错
  • ¥15 怎么让ai定时给我发信息 c#或者python