[英]php - count elements of array
我想知道到底我有多少成功和失敗。 我想使用數組函數,但我不知道如何從這里繼續:
public function array_internal($the_string)
$pass= Array();
$failed = Array();
if(strstr($the_string,"Success"))
{
$pass[] = +1;
}
else
{
$failed[] = +1;
}
count($pass);
此步驟正在運行每個斷言函數,如下所示:
try {
$this->assertEquals("off", $this->getValue("page"));
throw new PHPUnit_Framework_AssertionFailedError("Success");
} catch (PHPUnit_Framework_AssertionFailedError $e) {
$this->array_internal($e->toString());
}
該功能本身還可以。 我的問題僅在於櫃台。
謝謝!
編輯我試圖做這樣的事情:
$pass= 0;
$failed = 0;
public function array_internal($the_string)
if(strstr($the_string,"Success"))
{
$pass += 1;
}
else
{
$failed += 1;
}
$pass;
除了計數之外,您不對數組做任何事情,那么為什么不只使用整數呢?
$pass= 0;
$failed = 0;
public function array_internal($the_string)
global $pass, $failed;
if(strstr($the_string,"Success"))
{
$pass += 1;
}
else
{
$failed += 1;
}
}
為什么不只將全局變量用作$pass
和$fail
,您可以將它們分別增加$pass++
和$fail++
呢?
public function array_internal($the_string)
$pass=0;
$failed=0;
if (strstr($the_string,"Success"))
{
$pass += 1;
}
else
{
$failed += 1;
}
$pass[] = +1
在$pass
數組中創建一個新的鍵值對,並將1
加到新值上。 這可能不是您想要的。 查看您想做什么的其他答案。
$pass= Array();
$failed = Array();
創建新的數組實例。 函數array_internal
的返回值將始終為0或1。您也永遠不要使用$failed
。
一個簡單得多的函數是:
public function array_internal( $the_string )
$pass = 0;
if( strstr( $the_string, "Success" ) )
{
$pass = 1;
}
return $pass;
}
就像Harmen所說的,您需要使用一個外部int計數器。 與Harmen不同,我會盡量不要使用全局變量,而是使用類變量來限制其范圍。
也許是一個名為TestClass
的類的靜態變量,稱為$passes
例如:
TestClass::$passes += $this->array_internal($e->toString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.