PHP - Ds Sequence::contains() 函数



PHP 的Ds\Sequence::contains() 函数用于确定指定的值是否在序列中或包含这些值的序列中。

如果该值存在于序列中,则此函数返回布尔值'true',否则返回'false'。它允许您一次检查单个值或多个值。

语法

以下是 PHP Ds\Sequence::contains() 函数的语法:

public abstract bool Ds\Sequence::contains([ mixed $...values ] )

参数

以下是此函数的参数:

  • values - 需要检查的单个值或多个值。

返回值

如果提供的任何值都不在序列中,则此函数返回 false,否则返回 true。

示例 1

以下是 PHP Ds\Sequence::contains() 函数的基本示例:

<?php
   $seq = new \Ds\Vector([1, 2, 3, 4, 5]);
   echo "The sequence elements are: \n";
   print_r($seq);
   $val = 2;
   echo "The value needs to be checked: ".$val;
   
   #using contains() function
   echo "\nIs value ".$val." present in a sequence: ";
   var_dump($seq->contains($val));
?>

输出

执行上述程序后,将显示以下输出:

The sequence elements are:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The value needs to be checked: 2
Is value 2 present in a sequence: bool(true)

示例 2

如果序列包含指定的值,则此函数返回“false”。

以下是 PHP Ds\Sequence::contains() 函数的另一个示例。我们使用此函数来确定此序列(["Tutorials", "Point", "India"])是否包含值“Tutorix”:

<?php
   $seq = new \Ds\Vector(["Tutorials", "Point", "India"]);
   echo "The sequence elements are: \n";
   print_r($seq);
   $val = "Tutorix";
   echo "The value needs to be checked: ".$val;
   #using contains() function
   echo "\nIs value ".$val." present in a sequence: ";
   var_dump($seq->contains($val));
?>

输出

执行上述程序后,将生成以下输出:

The sequence elements are:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The value needs to be checked: Tutorix
Is value Tutorix present in a sequence: bool(false)

示例 3

一次检查多个值。

在下面的示例中,我们使用contains() 函数来确定指定的值是否存在于序列中:

<?php
   $seq = new \Ds\Vector(['a', 'b', 'c', 'd', 'e']);
   echo "The sequence elements are: \n";
   print_r($seq);
   echo "Is values 'a', 'b' and 'c' present in a sequence: ";
   var_dump($seq->contains('a', 'b', 'c'));
?>

输出

上述程序产生以下输出:

The sequence elements are:
Ds\Vector Object
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
)
Is values 'a', 'b' and 'c' present in a sequence: bool(true)
php_function_reference.htm
广告