in_array 值是唯一值。如何?

下面系列中的第二个 if 语句不断返回 true 并打印出即使estimateId不是数组中的唯一项。我该如何纠正?


目标是确保在下面的系列中始终只有一个 if 语句匹配。


例如,现在第二个 if 在第三个 if 的情况下返回 true。这不应该发生。


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

https://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>");


    }


慕容708150
浏览 111回答 2
2回答

绝地无双

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

千万里不及你

“目标是确保在下面的系列中始终只有一个 if 语句匹配。”只需使用 elseif 而不是 if then。<?php&nbsp;&nbsp; &nbsp; # estimates/active/&nbsp; &nbsp; if (in_array($_GET["estimatesFilter"], $_GET)) {&nbsp; &nbsp; &nbsp; &nbsp; print_r('estimatesFilter: ' . $_GET["estimatesFilter"] . ' url parameter(s) was passed.');&nbsp; &nbsp; &nbsp; print_r("<br>");&nbsp; &nbsp; }&nbsp; &nbsp; # This if keeps returning true even if estimateId is not the only item in the array?&nbsp; &nbsp; # estimates/1001/&nbsp; &nbsp; elseif (in_array($_GET["estimateId"], $_GET)) {&nbsp; &nbsp; &nbsp; &nbsp; print_r('estimateId: ' . $_GET["estimateId"] . ' url parameter(s) was passed.');&nbsp; &nbsp; &nbsp; &nbsp; print_r("<br>");&nbsp; &nbsp; }&nbsp; &nbsp; # estimates/1001/proposals/&nbsp; &nbsp; elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["proposalsFilter"], $_GET)) {&nbsp; &nbsp; &nbsp; &nbsp; print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'proposalsFilter: ' . $_GET["proposalsFilter"] . ' url parameter(s) was passed.');&nbsp; &nbsp; &nbsp; &nbsp; print_r("<br>");&nbsp; &nbsp; }&nbsp; &nbsp; # estimates/1001/proposals/3001/&nbsp; &nbsp; elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["proposalId"], $_GET)) {&nbsp; &nbsp; &nbsp; &nbsp; print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'proposalId: ' . $_GET["proposalId"] . ' url parameter(s) was passed.');&nbsp; &nbsp; &nbsp; &nbsp; print_r("<br>");&nbsp; &nbsp; }&nbsp; &nbsp; # estimates/1001/contracts/&nbsp; &nbsp; elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["contractsFilter"], $_GET)) {&nbsp; &nbsp; &nbsp; &nbsp; print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'contractsFilter: ' . $_GET["contractsFilter"] . ' url parameter(s) was passed.');&nbsp; &nbsp; &nbsp; &nbsp; print_r("<br>");&nbsp; &nbsp; }&nbsp; &nbsp; # estimates/1001/contracts/3001/&nbsp; &nbsp; elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["contractId"], $_GET)) {&nbsp; &nbsp; &nbsp; &nbsp; print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'contractId: ' . $_GET["contractId"] . ' url parameter(s) was passed.');&nbsp; &nbsp; &nbsp; &nbsp; print_r("<br>");&nbsp; &nbsp; }&nbsp; &nbsp; # estimates/1001/invoices/&nbsp; &nbsp; elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["invoicesFilter"], $_GET)) {&nbsp; &nbsp; &nbsp; &nbsp; print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'invoicesFilter: ' . $_GET["invoicesFilter"] . ' url parameter(s) was passed.');&nbsp; &nbsp; &nbsp; &nbsp; print_r("<br>");&nbsp; &nbsp; }&nbsp; &nbsp; # estimates/1001/invoices/4001/&nbsp; &nbsp; elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["invoiceId"], $_GET)) {&nbsp; &nbsp; &nbsp; &nbsp; print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'invoiceId: ' . $_GET["invoiceId"] . ' url parameter(s) was passed.');&nbsp; &nbsp; &nbsp; &nbsp; print_r("<br>");&nbsp; &nbsp; }&nbsp;?>
打开App,查看更多内容
随时随地看视频慕课网APP