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.

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

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.

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…


if ( in_array( $comment['profile_post'], $comment ) ) 

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

Crayon time!


$comments = array(); [COLOR="Red"]Empty array of output.[/COLOR]
                               
while( $comment = $queryResults() ) [COLOR="Red"]For every comment in our query result...[/COLOR]
  {
     if( in_array( $comment['profile_post'], array_keys( $comments ) ) [COLOR="Red"]If this profile_post (Which i assume is a UserID) has already commented on something...[/COLOR]
     {
        $comments[ $comment['profile_post'] ][] = $comment; [COLOR="Red"]Tack the comment on as an array element corresponding to his poster ID.[/COLOR]
      }
   else
    {
       $comments[ $comment['profile_post'] ] = array(); [COLOR="Red"]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.[/COLOR]
       $comments[ $comment['profile_post'] ][] = $comment; [COLOR="Red"]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.[/COLOR]
    }
 }

Thank you both for your replies. It makes sense now.