在这段代码中,我有一个for循环,它通过数组中的所有结果然后看起来用户输入等于现在是$testsubject
之后它检查凭证代码是否不是已过期,然后需要计算价格$testamount
echo的
我有这个foreach循环,我希望它首先循环通过数组中的所有项目,然后如果`$ testsubject等于其中一个,请检查下一个事项,如果它们都不相等,那么显示最后一次回声。
所以基本上如果($ testsubject == $ testVoucherArr ['code'])为true,请停止for each并检查下一个if语句,如果不是true则显示最后一次回显“这只需要显示什么时候没有结果“
的index.php
function display()
{
$arrContextOptions=array(
"ssl"=>array(
"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-B";
$testamount = "5,00";
$doesvoucherexists = false;
foreach($voucherList['data']['results'] as $testVoucher => $testVoucherArr){
if ($testsubject == $testVoucherArr['code']) {
$doesvoucherexists = true;
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 is expired";
};
break;
}
}
if ($doesvoucherexists === false) {
echo "this needs to only show when there are no results <br>";
}
if(isset($_POST['submit']))
{
display();
}}
答案 0 :(得分:1)
如果我理解你想做什么,你必须移动
if ($doesvoucherexists === false) {
在foreach循环之外。
在else ;
结束括号
}
的语法错误
告诉我以下代码是否适合您
function display()
{
$arrContextOptions = array(
"ssl" => array(
"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-B";
$testamount = "5,00";
$doesvoucherexists = false;
foreach($voucherList['data']['results'] as $testVoucher => $testVoucherArr) {
if ($testsubject == $testVoucherArr['code']) {
$doesvoucherexists = true;
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 is expired";
}
break;
}
}
if ($doesvoucherexists === false) {
echo "this needs to only show when there are no results <br />";
}
}
if (isset($_POST['submit'])) {
display();
}