0
我有一個有4個值的數組。我想在文本文件中搜索數組中的值並返回字符串可用的行號。但是,當我的代碼執行時,它只給出數組中第一個值的行號,並且還爲其餘的數組值打印相同的行號。在foreach中使用while循環來獲得一個字符串的行號
$scheme_code = array("106212","112422","114239","128053");
$search = $scheme_code[0];
$i = 0;
$line_number = false;
$count = 0;
$handle = fopen("http://portal.amfiindia.com/spages//NAV0.txt", 'r');
foreach ($scheme_code as $code) {
echo $code."<br>";
while (($line = fgets($handle, 4096)) !== FALSE and !$line_number) {
$count++;
$line_number = (strpos($line, $code) !== FALSE) ? $count : $line_number;
}
echo "The line number is".$line_number."<br>";
//fclose($handle);
}
我的輸出是這樣的106212 The line number is 5386 112422 The line number is 5386 114239 The line number is 5386 128053 The line number is 5386
5386 is the line number of the value 106212.
當我呼應$code
。它會打印$code
,但第一個代碼的行號只會打印其餘代碼。