그래, 문제가있다. 뉴스 피드를 설정하고 싶습니다. 그러면 사용자가 배치 한 시간까지 주문한 댓글과 댓글이 모두 표시됩니다. 나는이 모든 것을 할 수있는 함수를 만들었다. 다시 말하지만 그것이 가장 효율적인지는 모르겠지만 단지 효과를 얻고 싶습니다. 그래서 내가내 어레이와 관련된 문제
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
그때 나는 foreach 루프
foreach ($Class->foo() as $news)
{
echo $news["text"]
}
그러나 항상 foreach 루프의 두 개의 추가 빈 실행을 통해의를 제공 할 때마다 사용하여 전화를 가지고있는 것입니다. 또는 실제로 배열의 일부인지 모르겠다. foreach 루프에 문제가있는 경우 일반적으로 오류가 발생하기 때문입니다. array_merge() 함수에 문제가 있다고 생각하고 있지만 완전히 확실하지는 않습니다. 어떤 아이디어?
도움 주셔서 감사합니다. 정말 감사.
명백한 질문 : 당신은 (재) 사용하기 전에
당신은 항상 당신의 배열을 초기화한다 : //php.net/manual/en/function.array-merge.php)와'$ array1 + $ array2'? – ficuscr