mercredi 3 juillet 2019

Only adding Keys and Value to an Arrays when the If-condition is fulfilled

So im currently working on a PhP projekt. Now im trying to build an Array i want to output later on. The Array should only be filled with Value when the if-condition is true.

I cant seem to find any working code or any help. I don't really know what to try next.

$maxTestDuration = 20;

$startTime = strtotime("now");
$endTime = strtotime("+1 hour");
$testDuration = $endTime - $startTime;

$currentTest = "Test1";
$test2 = "test2";
$test3 = "test3";
$test4 = "test4";

$testDuration2 = "300";
$testDuration3 = "10";
$testDuration4 = "5420";


if ($testDuration > $maxTestDuration)
    {
        $overTimeTests =
        [
        $currentTest => $testDuration,
        "test2" =>  "300",
        "test3" => "2000",
        "test4" => "5420",
        ];
    }

foreach ($overTimeTests as $testName => $testDuration)
{
    $testText = $testName.": ".$testDuration." seconds\n";
    echo $testText;
}

The Result should be an Output of just the three tests that run under 20 seconds. Instead it shows all four tests. I also tried changing $endTime so that test1 would be faster then 20 seconds but then i get errors, that the array is empty.

Aucun commentaire:

Enregistrer un commentaire