我的数组问题

Ok, well I have an issue. I want to set up a news feed, and this will show both questions, and comments that a user has received ordered by the time they were placed. I set up a function to do all of this. Again I don't know if it's the most efficient but I just want to get it to work. So this is what I have

public function foo ()
{
    //Retrieve all of the questions
    $Statement = $this->Database->prepare("SELECT * FROM table WHERE condition = ?");
    $Statement->execute(array($this->variable));

    while ($row = $Statement->fetch(PDO::FETCH_ASSOC))
    {
        $id[]           = $row["id"];
        $question[]     = $row["question"];
        $asker[]        = $row["asker"];
        $timestamp[]    = $row["timestamp"];
        $likes[]        = $row["likes"];
    }

    $iLimit = count($id); //Set a limit based on the size of the array

    if ($iLimit > 0)
    {
                    //Save everything in an array for the questions
        for ($iCount = 0; $iCount < $iLimit; $iCount++)
        {
            $question[$iCount] = Array ( "id"           => $id[$iCount],
                                         "text"         => $question[$iCount],
                                         "username"     => $asker[$iCount],
                                         "timestamp"    => $timestamp[$iCount],
                                         "likes"        => $likes[$iCount] );
        }
    }

    //Retrieve all of the comments
    $Statement = $this->Database->prepare("SELECT * FROM table WHERE condition = ?");
    $Statement->execute(array($this->variable));

    while ($row = $Statement->fetch(PDO::FETCH_ASSOC))
    {
        $id[]           = $row["id"];
        $comment[]      = $row["comment"];
        $commenter[]    = $row["commenter"];
        $timestamp[]    = $row["timestamp"];
        $likes[]        = $row["likes"];
    }

    $iLimit = count($id); //Set a limit based on the size of the array

    if ($iLimit > 0)
    {
                    //Save everything in an array for comments
        for ($iCount = 0; $iCount < $iLimit; $iCount++)
        {
            $comment[$iCount] = Array ( "id"            => $id[$iCount],
                                        "text"          => $comment[$iCount],
                                        "username"      => $commenter[$iCount],
                                        "timestamp"     => $timestamp[$iCount],
                                        "likes"         => $likes[$iCount] );
        }
    }

    //Merge the two arrays
    $aNewsFeed = array_merge($question, $comment);

    foreach ($aNewsFeed as $row) 
    {
        $aOrdered[]  = $row["timestamp"];
    }

    array_multisort($aOrdered, SORT_DESC, $aNewsFeed); //Sort the array

    return $aNewsFeed;
} //end getNewsFeed

then I call it using a foreach loop

foreach ($Class->foo() as $news) 
{
    echo $news["text"]
}

But every time it always gives two additional blank run-through's of the foreach loop. Or I don't know if it's actually part of the array, because usually it would send out an error if it was a problem with the foreach loop. I'm thinking it has to be a problem with the array_merge() function, but I'm not completely sure. Any ideas?

Thank you for the help. I really appreciate it.

It seems you are using the arrays $id, $likes, etc. as temporary variables but you are not resetting them before you use them the second time so your $id array will have questions in them when you start using it for the comments.

You should always initialize your arrays before you (re-) use them:

$id = array();
// first loop

$id = array();
// second loop
foreach ($Class->foo() as $news) 
{
    if($news["text"]!=""){
        echo $news["text"];
    }
}