PHP - Ds Vector::contains() 函式



PHP 的 Ds\Vector::contains() 函式用於確定指定值是否出現在向量中。此函式可以一次檢查多個值,以查看向量中是否存在任何值。

如果給定值出現在向量中,此函式返回布林值“true”,否則返回“false”。

語法

以下是 PHP Ds\Vector::contains() 函式的語法:

public Ds\Vector::contains(mixed ...$values): bool

引數

以下是此函式的引數:

  • values − 需要檢查的單個或多個值。

返回值

如果提供的 value 出現在向量中,此函式返回“true”,如果任何一個提供的 value 不在向量中,則返回“false”。

示例 1

如果指定的值出現在當前向量中,則 PHP Ds\Vector::contains() 函式返回“true”:

<?php 
   $vector = new \Ds\Vector([1, 2, 3, 4, 5]);
   echo "The vector elements are: \n";
   print_r($vector);
   $value = 1;
   echo "The given value is: ".$value;
   echo "\nIs the value ".$value." is present in a vector? ";
   #using contains() function
   var_dump($vector->contains($value));
?>

輸出

執行上述程式後,將顯示以下輸出:

The vector elements are:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The given value is: 1
Is the value 1 is present in a vector? bool(true)

示例 2

如果指定的值不存在於向量中,則此函式返回“false”。

以下是 PHP Ds\Vector::contains() 函式的另一個示例。我們使用此函式來確定指定值“Tutorix”是否出現在向量中:

<?php 
   $vector = new \Ds\Vector(["tutorials", "point", "India"]);
   echo "The vector elements are: \n";
   print_r($vector);
   $value = "Tutorix";
   echo "The value need to check: ".$value;
   echo "\nIs the value ".$value." is present in a vector? ";
   #using contains() function
   var_dump($vector->contains($value));
?>

輸出

上述程式產生以下輸出:

The vector elements are:
Ds\Vector Object
(
    [0] => tutorials
    [1] => point
    [2] => India
)
The value need to check: Tutorix
Is the value Tutorix is present in a vector? bool(false)

示例 3

一次檢查多個值。

在下面的示例中,我們使用contains()函式來確定值'a','b','d','e'是否出現在向量中:

<?php 
   $vector = new \Ds\Vector(['a', 'e', 'i', 'o', 'u']);
   echo "The vector elements are: \n";
   print_r($vector);
   $v1 = 'a';
   $v2 = 'b';
   $v3 = 'd';
   $v4 = 'e';
   echo "The given values: ".$v1.", ".$v2.", ".$v3.", ".$v4;
   echo "\nIs all the values are present in a vector? ";
   #using contains() function
   var_dump($vector->contains($v1, $v2, $v3, $v4));
?>

輸出

執行上述程式後,將顯示以下輸出:

The vector elements are:
Ds\Vector Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The given values: a, b, d, e
Is all the values are present in a vector? bool(false)
php_function_reference.htm
廣告