为了账号安全,请及时绑定邮箱和手机立即绑定

in_array 值是唯一值。如何?

in_array 值是唯一值。如何?

PHP
慕容708150 2022-10-14 10:03:22
下面系列中的第二个 if 语句不断返回 true 并打印出即使estimateId不是数组中的唯一项。我该如何纠正?目标是确保在下面的系列中始终只有一个 if 语句匹配。例如,现在第二个 if 在第三个 if 的情况下返回 true。这不应该发生。https://localhost/data.php?estimateId=1001https://localhost/data.php?estimateId=1001&proposalsFilter=all.<?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/    if (in_array($_GET["estimateId"], $_GET)) {        print_r('estimateId: ' . $_GET["estimateId"] . ' url parameter(s) was passed.');        print_r("<br>");    }    # estimates/1001/proposals/    if (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/    if (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/    if (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/    if (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>");    }
查看完整描述

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。

https://localhost/data.php?estimateId=1001

上面返回 true 因为 $_GET 只包含一个值。

https://localhost/data.php?estimateId=1001&proposalsFilter=all

上面返回 false 因为 $_GET 数组包含 2 个值。


查看完整回答
反对 回复 2022-10-14
?
千万里不及你

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>");


    }


 ?>



查看完整回答
反对 回复 2022-10-14
  • 2 回答
  • 0 关注
  • 110 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号