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

时间:2013-11-12 16:08:46

标签: php arrays

我有一个名为$topic_notes的数组如下:

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] => 
        )

)

现在我想在数组$topic_notes中的每个内部数组中创建一个新的键值对,但我无法做到。我尝试过的代码如下:

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;     
  }

执行此代码后,我得到以下输出:

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

    )

新的键值对(即新数组元素)到达最后位置。实际上我想在每个(所有10个元素)数组元素中都有这个元素。

4 个答案:

答案 0 :(得分:2)

您需要在循环中获取索引并在那里设置值,或者只在for循环中使用引用。默认情况下,for循环中的所有项都是其原始数组的副本,因此不会反映对其迭代元素所做的更改。

通过索引

foreach ($topic_notes as $idx => $topic)
{
    // SNIP
    $topic_notes[$idx]['subject'] = $subject_name;     
}

通过参考

foreach ($topic_notes as &$topic)
{
    // SNIP
    $topic['subject'] = $subject_name;     
}

在您的原始示例中,您也没有操纵子数组,而是父数组!

答案 1 :(得分:1)

试试这个:

foreach ($topic_notes as $index => $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[$index]['subject'] = $subject_name;     
  }

答案 2 :(得分:1)

你最好只在第一个查询(JOIN)中从数据库中获取所有内容,但如果不可能,请使用引用然后更改:

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 :(得分:0)

你的意思是:

foreach ($topic_notes as $key => $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[$key]['subject'] = $subject_name;     
  }

除了: 小心那个字符串连接 - 有一个很好的SQL注入错误等待发生....

相关问题