首页 > 解决方案 > 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>");

    }

    # estimates/1001/invoices/
    if (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/
    if (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>");

    }

 ?>

标签: php

解决方案


# 即使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 个值。


推荐阅读