SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question retrieving data into a two-dimensional array

    I want to retrieve my data into a two-dimensional array that I can navigate through with a foreach loop, and be able to add new fields to the array afterwords.

    For example
    Code:
    foreach($query_results as $qr)
    {
         $qr->brand_new_field = retrieve_other_data($qr->first_field, $qr->second_field);
    }
    I`m going around in circles with this but I can`t quite seem to get it right. Any suggestions?

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    In a foreach loop, the "as' portion is actually a temporary variable built for that instance, assigned to the value from the subject array. Therefore, modifying $qr won't do anything, as it's not part of the array. Does that make sense?

    foreach($query_results as $k=>$qr)
    {
    $query_results[$k]->brand_new_field = retrieve_other_data($qr->first_field, $qr->second_field);
    }

    Try this.

    Also this is off topic, but if you are doing queries within that loop, you are doing it terribly wrong. But that's for another thread.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •