2011-08-26 146 views
2

好吧,我很困惑。php foreach在第一次迭代後停止

我有一個multidemionsal數組,其中有660條記錄。當我運行我的foreach時,每次傳球都會得到一條記錄,但我得到的只是第一條記錄。我提高了我的錯誤報告,我沒有得到任何回報,我沒有在foreach中分配任何內存(只是輸出表格內容)。

代碼如下所示:

$totCount = count($funnel); 

echo "We found $totCount log entries<br><br>\n"; // 66x records! 
echo "<table border='1'>\n"; 
echo "<tr><th>Visitor</th><th>Date</th><th>Time/Page ---></th></tr>\n"; 
$nCount = 0; // number of records 
$nRec = 0; 
$ip = ""; // current records ip address 
$date = ""; // current records date 
$time = ""; // current records time 
$file = ""; // file that was viewed 

foreach($funnel as $page); 
{ 
    // process each record 
    $nRec++; 

    // if the ip's are different, this is a new visitor 
    if(strcmp($page['ip'], $ip) != 0) 
    { 
    if($nCount > 0) // only end a row if its not the first one 
     echo "</tr>\n"; // close out this row 

    // update the variables 
    $ip = $page['ip'];  // save the new visitors ip 
    $date = $page['date']; // save the new visitors date 
    $time = $page['time']; // save the new visitors time 
    $file = $page['path']; // save the new visitors file viewed 

    echo "<tr>\n"; // start a new row 
    echo "<td>$ip</td><td>$date</td>\n"; 
    echo "<td>$time<br>$file</td>"; 

    $nCount++;   
    } 
    else 
    { 
    // not a new visit 
    $time = $page['time']; // what time was this page viewed? 
    $file = $page['path']; // where did they go next? 
    echo "<td>$time<br>$file</td>"; 
    } 
} 
echo "</table>"; 
echo "<br>Processing Complete. $nCount visitors of $totCount/$nRec"; 

上面一行說我有66X 1名訪客,NREC是1顯示的foreach執行1次。

任何幫助,將不勝感激!

+2

,你在哪裏得到$渠道? – LeleDumbo

+0

是的,您需要確保$ funnel確實是一個包含多個條目的數組。 – chaoskreator

回答

4

foreach後的冒號?

foreach($funnel as $page); 

應該是:

foreach($funnel as $page) 
+2

是的......我無法相信我錯過了!而且我甚至不是一個新手! LOL – ppetree

+1

我不認爲這種情況會消失。第二組眼睛總是似乎神奇地幫助。 – wkm

+1

男人,這發生在我身上的15分鐘循環和這個評論使魔法不可思議的半結腸出現! – Tek

1

卸下 ';'在您的foreach聲明之後 - 它基本上消除了它。

+0

我的意思是在早些時候回覆你...... DAMN!我不能相信我錯過了那個分號!我將循環切換到for(;;)並在第一次運行後找到它。 – ppetree

0

它看起來像我,因爲你有一個多維數組,但你沒有通過每個條目。

例子:

<?php 
/*------------------*/ 
//Normal array - As it seems your treating it in your example 
$array = array('ip'=>$_SERVER['REMOTE_ADDR'],'date'=>'21/01/2011','time'=>time(),'path'=>'./test'); 
foreach($array as $key=>$item) 
{ 
    echo $key.'-'.$item.'<br>'; 
} 
/*------------------*/ 

/*------------------*/ 
//Multidimensional 
$array = array(
array('ip'=>$_SERVER['REMOTE_ADDR'],'date'=>'21/01/2011','time'=>time(),'path'=>'./test/1'), 
array('ip'=>$_SERVER['REMOTE_ADDR'],'date'=>'21/01/2011','time'=>time(),'path'=>'./test/2'), 
array('ip'=>$_SERVER['REMOTE_ADDR'],'date'=>'21/01/2011','time'=>time(),'path'=>'./test/3'), 
array('ip'=>$_SERVER['REMOTE_ADDR'],'date'=>'21/01/2011','time'=>time(),'path'=>'./test/4'), 
array('ip'=>$_SERVER['REMOTE_ADDR'],'date'=>'21/01/2011','time'=>time(),'path'=>'./test/5'), 
); 

foreach($array as $entry) 
{ 
    foreach($entry as $key=>$item) 
    { 
     echo $key.'-'.$item.'<br>'; 
    } 

} 
/*------------------*/ 
?> 
相關問題