Php 首先循环所有事情,如果1是真的,做点什么

Php 首先循环所有事情,如果1是真的,做点什么,php,arrays,loops,Php,Arrays,Loops,所以我有一个foreach循环,它检查$testsubject是否等于数组的结果 但是我想让它先检查所有的结果,如果其中一个结果是真的,那么就进一步检查日期,否则就重复凭证不正确 代码的用途是,用户输入凭证代码,目前为$testsubject,然后我检查凭证是否存在于系统中如果是真的,我使用日期功能检查凭证是否过期,然后我削减价格的折扣$testamount 回声的图像 index.php 函数显示() { $arrContextOptions=[ “ssl”=>[ “验证对等”=>错误, “验

所以我有一个foreach循环,它检查
$testsubject
是否等于数组的结果

但是我想让它先检查所有的结果,如果其中一个结果是真的,那么就进一步检查日期,否则就重复凭证不正确

代码的用途是,用户输入凭证代码,目前为
$testsubject
,然后我检查凭证是否存在于系统中如果是真的,我使用日期功能检查凭证是否过期,然后我削减价格的折扣
$testamount

回声的图像

index.php

函数显示()
{
$arrContextOptions=[
“ssl”=>[
“验证对等”=>错误,
“验证对等名称”=>false,
],
];
$getVoucherList=”https://www.planyo.com/rest/?method=list_vouchers&api_key=yourkey&resource_id=110556";
$cleanVoucherList=preg_replace(“//”、“%20”、$getVoucherList);
$voucherlist=file\u get\u contents($cleanVoucherList),false,stream\u context\u create($arrContextOptions));
$voucherList=json_decode($voucherList,true);
$testsubject=“TESTVOUCHER”;
$testamount=“5,00”;
foreach($TestVoucherList['data']['results']作为$testVoucher=>$testVoucherArr){
if($testsubject==$testVoucherArr['code'])){
echo$testsubject.“不等于“$testVoucherArr['code']”。”
; echo$testVoucherArr['rental_end_date']。“
”; echo$testVoucherArr[“折扣值”]。“
”;
如果(日期(“Y-m-d”)首先将标志设置为
true
,然后在出现错误时循环将标志设置为
false
。然后测试标志:

$flag = true;                          // SET A FLAG
foreach($a as $b){
    if($b !== 'Hello')$flag = false;   // IF contidtion not met, set flag to true
    }
if($flag === false){                   // TEST IF flag result
    echo 'Dear oh dear';die;
    }
foreach(....){                         // GO ON if flag === true
    ....
    }

如果代码有效,则输入一个检查日期的函数。函数结束后,foreach循环将使用“break;”结束

函数testVoucherDate($凭证)
{
如果(日期(“Y-m-d”)$testVoucherArr{
if($testsubject==$testVoucherArr['code'])){
echo$testsubject.“不等于“$testVoucherArr['code']”。”
; echo$testVoucherArr['rental_end_date']。“
”; echo$testVoucherArr[“折扣值”]。“
”; testVoucherDate($testVoucherArr); 打破 }否则{ echo$testsubject.“不等于”。 $testVoucherArr['code']。“
”; } }

编辑:我已将函数置于循环之上,因此不会出现未定义函数的错误

为什么不在任何地方解释代码的用途以及它的全部含义?不在您的问题中,也不在您的代码中。将变量设置为
false
,如果任何项目与您的条件集相匹配,则循环您的项目将aid变量设置为
true
并停止循环。然后检查是否存在此问题variable@KIKOSoftware我更新了postI获取错误:Parse error:syntax error,意外的“=>”(T_DOUBLE_ARROW)啊抱歉,必须是
as
foreach
中,现在我得到这个警告:为foreach()提供的参数无效我在尝试时遇到了这个错误:致命错误:调用未定义的函数testVoucherDate(),我修复了它,只是将函数移到了foreach之上。
$flag = true;                          // SET A FLAG
foreach($a as $b){
    if($b !== 'Hello')$flag = false;   // IF contidtion not met, set flag to true
    }
if($flag === false){                   // TEST IF flag result
    echo 'Dear oh dear';die;
    }
foreach(....){                         // GO ON if flag === true
    ....
    }
function testVoucherDate($voucher)
{
    if (date("Y-m-d") <= $testVoucherArr['rental_end_date']) {
        echo "this code can be used <br>";
        echo $testamount - $testVoucherArr['discount_value'] . "<br>";
    } else {
        echo "this code cannot be used";
    };
}

foreach ($voucherList['data']['results'] as $testVoucher => $testVoucherArr) {
    if ($testsubject == $testVoucherArr['code']) {
        echo $testsubject . " is not equal to " . $testVoucherArr['code'] . "<br>";
        echo $testVoucherArr['rental_end_date'] . "<br>";
        echo $testVoucherArr['discount_value'] . "<br>";
        testVoucherDate($testVoucherArr);
        break;
    } else {
        echo $testsubject . " is not equal to " .
            $testVoucherArr['code'] . "<br>";
    }
}