-1

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.

afuzzyllama
  • 6,275
  • 5
  • 42
  • 61
PHPLover
  • 7,021
  • 32
  • 90
  • 182
  • You're writing to the wrong reference in your `foreach` loop. Instead of `$topic_notes['subject'] = $subject_name;`, use `$topic['subject'] = $subject_name;` and you should be all set, since you're iterating it as `$topic`. Note: you'll also need to `foreach($topic_notes as &$topic)` to write to the iterator. – Michael Berkowski Nov 12 '13 at 16:11
  • Why not `JOIN` the tables and get all the info you need in one query from the very start? – AbraCadaver Nov 12 '13 at 16:21
  • possible duplicate of [Insert new item in array on any position in PHP](http://stackoverflow.com/questions/3797239/insert-new-item-in-array-on-any-position-in-php) – Valentin Despa Mar 01 '14 at 09:29

4 Answers4

2

You need to either get the index within your loop and set the value there or just use a reference within your for loop. By default, all items within the for loop are copiesof their original array, so changes made to it's iterated element wont be reflected.

via index

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

via reference

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

In your original example you were also not manipiulating the sub array but the parent array!

ToBe
  • 2,589
  • 1
  • 14
  • 28
1

Try this:

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

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'];     
  }
AbraCadaver
  • 73,820
  • 7
  • 55
  • 81
0

Do you mean:

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

Aside: Be careful with that string concatenation - there's a nice SQL injection bug waiting to happen there....

Rob Baillie
  • 3,248
  • 2
  • 18
  • 32