2 回答

TA贡献1946条经验 获得超4个赞
# 即使estimateId 不是数组中的唯一项,这个if 仍然返回true?
# 估计/1001/
要解决您的问题,您可以检查该值是否是唯一值
if(in_array($_GET["estimateId"], $_GET))
变成:
if(\count($_GET) === 1 && \in_array($_GET["estimateId"], $_GET) ){
/***
this will only trigger if `$_GET["estimateId"]` is in the
array $_GET and is the only value in the array.
***/
}
它检查值并检查count
数组中的值。因为您正在寻找这个值,所以数组计数应该只有 1。
上面返回 true 因为 $_GET 只包含一个值。
https://localhost/data.php?estimateId=1001&proposalsFilter=all
上面返回 false 因为 $_GET 数组包含 2 个值。

TA贡献1784条经验 获得超9个赞
“目标是确保在下面的系列中始终只有一个 if 语句匹配。”
只需使用 elseif 而不是 if then。
<?php
# estimates/active/
if (in_array($_GET["estimatesFilter"], $_GET)) {
print_r('estimatesFilter: ' . $_GET["estimatesFilter"] . ' url parameter(s) was passed.');
print_r("<br>");
}
# This if keeps returning true even if estimateId is not the only item in the array?
# estimates/1001/
elseif (in_array($_GET["estimateId"], $_GET)) {
print_r('estimateId: ' . $_GET["estimateId"] . ' url parameter(s) was passed.');
print_r("<br>");
}
# estimates/1001/proposals/
elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["proposalsFilter"], $_GET)) {
print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'proposalsFilter: ' . $_GET["proposalsFilter"] . ' url parameter(s) was passed.');
print_r("<br>");
}
# estimates/1001/proposals/3001/
elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["proposalId"], $_GET)) {
print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'proposalId: ' . $_GET["proposalId"] . ' url parameter(s) was passed.');
print_r("<br>");
}
# estimates/1001/contracts/
elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["contractsFilter"], $_GET)) {
print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'contractsFilter: ' . $_GET["contractsFilter"] . ' url parameter(s) was passed.');
print_r("<br>");
}
# estimates/1001/contracts/3001/
elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["contractId"], $_GET)) {
print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'contractId: ' . $_GET["contractId"] . ' url parameter(s) was passed.');
print_r("<br>");
}
# estimates/1001/invoices/
elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["invoicesFilter"], $_GET)) {
print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'invoicesFilter: ' . $_GET["invoicesFilter"] . ' url parameter(s) was passed.');
print_r("<br>");
}
# estimates/1001/invoices/4001/
elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["invoiceId"], $_GET)) {
print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'invoiceId: ' . $_GET["invoiceId"] . ' url parameter(s) was passed.');
print_r("<br>");
}
?>
- 2 回答
- 0 关注
- 110 浏览
添加回答
举报