2014-11-05 4 views
-4

코드를 수행하고 있어도이 오류가 발생합니다. 코드별로 줄을 설명하려고합니다.PHP 정의되지 않은 오프셋 : 5 행의 [파일 위치] - 다른 답변으로 응답하지 않음

$bits = array();//bits is the array that I use to hold the array that explodes each line 
$explParts = array(); 
$parts = NULL;//variable that holds the 6 item in array (0 index so later i use parts[5]) 
$articleCount = 0;//variable that holds the counter for article 
foreach ($files as $dataz) { //loop that goes through all files in folder 
    $handle = fopen('data/'.$dataz, 'r');//open the first file, and second after it finished the first 
    while (!feof($handle)){//while not end of file {do} 
    $newLine = fgets($handle);//$newLine holds the *(1,2,3) line;     
    $bits = explode(" ", $newLine);//$bits hold the array that is created after the explosion of first line 
    //print_r(array_values($bits));//uncomment this if you want to see what are the values inside bits; 
    $parts = $bits[5];//$parts is the variables that holds the 6th item in the $bits array 
    $explParts = explode("/", $parts); //$explParts is the array that holds the items from parts that are exploded 
    $findArticle = $explParts[0];//$findArticle holds the 1st ideam in $explPart array. 
    $findArticle = trim($findArticle);//trimmed the value so spaces are deleted. 
    if ($findArticle == "articles") {//if what's inside $findArticle == "articles", add 1 to the counter 
     $articleCount++; 
    } 
    } 
} 
echo '<p> The number of requests from the articles directory is: '.$articleCount.'</p>'; //displays the total number of request from the articles directory     
fclose($handle);//close $handle 

문제는 그 파일의 corrent 번호를 심지어 네가 내가 파일에이 오류가 있다는 것입니다 :

나는 $ 부품에 사용 (5)에 문제가있는 것처럼 보이는
Notice: Undefined offset: 5 in /home/otoma01/public_www/p1tma/trytask2.php on line 38 
Call Stack: 0.0011 332600 1. {main}() /home/otoma01/public_www/p1tma/trytask2.php:0 
Notice: Undefined offset: 5 in /home/otoma01/public_www/p1tma/trytask2.php on line 38 
Call Stack: 0.0011 332600 1. {main}() /home/otoma01/public_www/p1tma/trytask2.php:0 
Notice: Undefined offset: 5 in /home/otoma01/public_www/p1tma/trytask2.php on line 38 
Call Stack: 0.0011 332600 1. {main}() /home/otoma01/public_www/p1tma/trytask2.php:0 
Notice: Undefined offset: 5 in /home/otoma01/public_www/p1tma/trytask2.php on line 38 
Call Stack: 0.0011 332600 1. {main}() /home/otoma01/public_www/p1tma/trytask2.php:0 

= $ 비트 [5].

하지만 그 배열에서 가져 가고 싶은 부분의 색인 일뿐입니다.

+0

어떻게'$ files' 변수가 선언 되었습니까? 주요 문제 일 수 있습니다. – cuSK

+0

38 줄은 무엇입니까? – Mike

+1

분명히'explode()'에서 나오는'$ bits' 배열은 (indexes 0 -> 5)에 6 개의 원소를 가지고 있지 않습니다. 정말로 'var_dump ($ 비트)'를 써서 정말로 당신이 얻은 것을보십시오. –

답변

0

해답으로 누구나 쉽게 의견을 말하게되었습니다.

$bits = array(); 
$articleCount = 0; 
foreach ($files as $dataz) { 
    $handle = fopen('data/'.$dataz, 'r'); 
     while (!feof($handle)){ 
     $newLine = fgets($handle);     
     $bits = explode(" ", $newLine); 
     if (empty($newLine)) { 
      continue; 
     } 
     $parts = $bits[5]; 
     $explParts = explode("/", $parts); 
     $findArticle = $explParts[0]; 
     $findArticle = trim($findArticle); 
     if ($findArticle == "articles") { 
     $articleCount++; 
     } 
    } 
}  
echo '<p> The number of requests from the articles directory is: '.$articleCount.'</p>';     
fclose($handle);