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条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题