2011-08-26 8 views
2

OK、私はうんざりです。php foreachは最初の繰り返しの後で停止します

私はそれに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は、foreachのは1度に実行示す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

私は以前にあなたに返答するつもりだった...ダン!私はそのセミコロンを逃したとは思えません!ループを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>'; 
    } 

} 
/*------------------*/ 
?> 
関連する問題