-1

我有一个命名$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

4 回答 4

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

在您的原始示例中,您也不是在操作子数组,而是在操作父数组!

于 2013-11-12T16:14:03.440 回答
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;     
  }
于 2013-11-12T16:13:11.920 回答
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'];     
  }
于 2013-11-12T16:16:56.680 回答
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 注入错误等待发生......

于 2013-11-12T16:12:58.033 回答