【问题标题】:How to check if an associative array contains a value and only that value?如何检查关联数组是否包含一个值并且仅包含该值?
【发布时间】:2019-10-03 13:13:17
【问题描述】:

我需要检查关联数组是否包含某个值并且仅包含该值。所以例如键choice需要包含值Afhalen

下面是一个示例数组:

Array
(
    [Test product1644] => Array
        (
            [artikelid] => 644
            [product] => Test product
            [price] => 20,00
            [picture] => images/_bad_noimg.jpg
            [quantity] => 1
            [alias] => test-product-2
            [catalias] => stormbanen-huren
            [prodoptie] => 1644
            [choice] => Bezorgen
        )

    [Test product2644] => Array
        (
            [artikelid] => 644
            [product] => Test product
            [price] => 90,00
            [picture] => images/_bad_noimg.jpg
            [quantity] => 1
            [alias] => test-product-2
            [catalias] => stormbanen-huren
            [prodoptie] => 2644
            [choice] => Bezorgen & Opbouw
        )

    [Test product3644] => Array
        (
            [artikelid] => 644
            [product] => Test product
            [price] => 100,00
            [picture] => images/_bad_noimg.jpg
            [quantity] => 1
            [alias] => test-product-2
            [catalias] => stormbanen-huren
            [prodoptie] => 3644
            [choice] => Bezorgen & Afhalen
        )

    [Test product4644] => Array
        (
            [artikelid] => 644
            [product] => Test product
            [price] => 200,00
            [picture] => images/_bad_noimg.jpg
            [quantity] => 1
            [alias] => test-product-2
            [catalias] => stormbanen-huren
            [prodoptie] => 4644
            [choice] => Afhalen
        )

)

上面的数组应该返回 false,因为有更多的 choice 键的值不是 Afhalen

下面的数组应该返回真,因为choice 总是包含Afhalen

Array
(
    [Test product4644] => Array
        (
            [artikelid] => 644
            [product] => Test product
            [price] => 200,00
            [picture] => images/_bad_noimg.jpg
            [quantity] => 1
            [alias] => test-product-2
            [catalias] => stormbanen-huren
            [prodoptie] => 4644
            [choice] => Afhalen
        )

    [Test product4646] => Array
        (
            [artikelid] => 649
            [product] => Test product
            [price] => 200,00
            [picture] => images/_bad_noimg.jpg
            [quantity] => 1
            [alias] => test-product-2
            [catalias] => stormbanen-huren
            [prodoptie] => 4644
            [choice] => Afhalen
        )
)

我发现了一个关于如何使用 javascript 执行此操作的问题,但没有找到关于如何使用 PHP 执行此操作的问题。

【问题讨论】:

    标签: php arrays


    【解决方案1】:
    $hasOnlySingleChoice = true;
    foreach ($array as $item) {
        if ($item['choice'] !== 'Afhalen') {
            $hasOnlySingleChoice = false;
            break;
        }
    }
    

    【讨论】:

      【解决方案2】:
      function distinctValue($array, $value){
             foreach($array as $item){
                   if( $item[‘choice’] != value ) {
                        return false;
                    }
             }
             return true;
        }
      

      【讨论】:

        【解决方案3】:

        你可以使用array_filter来实现这个,

        $a = array_filter($array, function($value,$key) {
            return $item['choice'] != 'Afhalen'; // filter all which are not equal to 'Afhalen'
        });
        echo (count($a) > 0 ? false : true); // if there are values with choice  
                                                //other than Afhalen then false else true
        

        【讨论】:

          猜你喜欢
          • 2021-06-06
          • 2019-08-10
          • 2015-01-26
          • 2014-09-05
          • 1970-01-01
          • 2012-01-10
          • 1970-01-01
          • 2015-01-01
          • 2021-07-15
          相关资源
          最近更新 更多