2013-04-11 180 views
0

我正在开发一个网站,需要在foreach循环中获取数组的总行数。PHP - 在foreach循环中获取总行

这里是我的代码:

$vr = $_GET['vr']; 

$dat = file_get_contents("http://example.com"); //this is a datafeed url. 

$exp = explode("\n", $dat); 

foreach ($exp as $val) { 

if($vr == $val) { 
// here I have the code if string is found in array. They were found, but I want to get how many were found. 
} 

我试过count();功能,但它显示111111111111111111

我知道这是可能的,但我不知道如何做到这一点。

回答

2
$num = 0; 
foreach ($exp as $val) { 
    if($vr == $val) { 
     $num++; 
    } 
} 
echo $num; 
+0

谢谢你真的工作。 :) – user2271258 2013-04-11 16:40:44