所以我有这个foreach循环检查$testsubject
是否等于数组的结果。
但我希望它首先检查所有结果,如果一个是真的,那么进一步检查日期,否则只是回应凭证不是核心。
代码的目的是用户输入的凭证代码现在为$testsubject
,而不是我检查系统中是否存在凭证如果是,我检查它是否与日期一起过期功能,然后我削减了价格$testamount
的折扣。
回声的https://imagebin.ca/v/3xQuiAClVsAG
的图像的index.php
function display()
{
$arrContextOptions = [
"ssl" => [
"verify_peer" => false,
"verify_peer_name" => false,
],
];
$getVoucherList = "https://www.planyo.com/rest/?method=list_vouchers&api_key=yourkey&resource_id=110556";
$cleanVoucherList = preg_replace("/ /", "%20", $getVoucherList);
$voucherlist = file_get_contents("$cleanVoucherList", false, stream_context_create($arrContextOptions));
$voucherList = json_decode($voucherlist, true);
$testsubject = "TESTVOUCHER";
$testamount = "5,00";
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>";
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";
}
;
} else {
echo $testsubject . " is not equal to " .
$testVoucherArr['code'] . "<br>";
}
}
}
if (isset($_POST['submit'])) {
display();
}
答案 0 :(得分:1)
首先将标志设置为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
....
}
答案 1 :(得分:1)
这对你有用吗?如果代码有效,则输入检查日期的函数。函数结束后,foreach循环将以“break;”
结束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>";
}
}
编辑:我把这个函数放在循环上面,所以不会出现未定义函数的错误