SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Populating Nested Arrays through While Loop

    This bit of code is confusing me, particularly the if() clause inside the while loop.

    We're looping through an associative array of query results. But the first thing we do within the iteration is check if $comment['profile_post'] is also present in our $comments[] array.

    This makes no sense to me as we've just instantiated the $comments array so from my understanding this if() check is designed to fail every time... Or is it just designed to fail upon first iteration?

    Also what confuses me is whether the if() check fails or not we feed the $comment[] array back into itself.


    Code PHP:
    $comments = array();
     
    while( $comment = $queryResults() )
      {
         if( in_array( $comment['profile_post'], array_keys( $comments ) )
         {
            $comments[ $comment['profile_post'] ][] = $comment;
          }
       else
        {
           $comments[ $comment['profile_post'] ] = array();
           $comments[ $comment['profile_post'] ][] = $comment;
        }
     }
    Last edited by Mittineague; Jul 24, 2011 at 21:11. Reason: reformatting bbcode tags

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If the array does not exist, you initialize the variable to be an empty array, then push the comment onto that array.

    If the array exists because you've already done the initialization just mentioned, you push the comment onto the existing array.

    The body of your if/else are not the same. The condition will fail only when the value of $comment['profile_post'] has never come up in a previous iteration of the loop.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This was beginning to make sense to me until I realized that $comments will never be a key in the array. It is the array variable itself.

    for the if(in_array()) check to work wouldn't it have to be written...

    PHP Code:
    if ( in_array$comment['profile_post'], $comment ) ) 

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The check is whether the value of $comment['profile_post'] is a key in the array $comments.

    $comments is the multidimensional array you're building

    $comment is a single comment from the database, different in each iteration of the loop

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Crayon time!
    Code:
    $comments = array(); Empty array of output.
                                   
    while( $comment = $queryResults() ) For every comment in our query result...
      {
         if( in_array( $comment['profile_post'], array_keys( $comments ) ) If this profile_post (Which i assume is a UserID) has already commented on something...
         {
            $comments[ $comment['profile_post'] ][] = $comment; Tack the comment on as an array element corresponding to his poster ID.
          }
       else
        {
           $comments[ $comment['profile_post'] ] = array(); He hasnt posted anything yet, but he is now. So we create an empty array to hold his comments, using his profile_post as a key.
           $comments[ $comment['profile_post'] ][] = $comment; And tack the first comment he made (the current one) into it. 
    Note that this could have been condensed into a single line by pushing $comment into the array definition above.
        }
     }

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you both for your replies. It makes sense now.


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
  •