如何检查关联数组是否包含一个值并且仅包含该值?

时间:2019-05-16 09:40:12

标签: php arrays

我需要检查关联数组是否包含某个值并且仅包含该值。例如,键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
        )

)

由于存在更多的choice键而不是Afhalen的键,因此上述数组应返回false。

由于choice始终包含Afhalen,因此下面的数组应返回true:

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做到这一点的问题。

3 个答案:

答案 0 :(得分:1)

$hasOnlySingleChoice = true;
foreach ($array as $item) {
    if ($item['choice'] !== 'Afhalen') {
        $hasOnlySingleChoice = false;
        break;
    }
}

答案 1 :(得分:1)

function distinctValue($array, $value){
       foreach($array as $item){
             if( $item[‘choice’] != value ) {
                  return false;
              }
       }
       return true;
  }

答案 2 :(得分:1)

您可以使用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