PHP - 获取foreach循环中的总行数

时间:2013-04-11 16:36:12

标签: 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

我知道这是可能的,但我不知道如何制作它。

1 个答案:

答案 0 :(得分:2)

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