PHP - Ds Sequence::find() 函式



PHP 的 Ds\Sequence::find() 函式用於在序列中查詢指定值的索引。此函式返回給定值的索引,如果在序列中找不到該值,則返回“false”。

索引是序列中元素的位置,從“0”開始,索引“0”表示第一個元素,“1”表示第二個元素,依此類推。

語法

以下是 PHP Ds\Sequence::find() 函式的語法:

public abstract mixed Ds\Sequence::find( mixed $value )

引數

以下是此函式的引數:

  • value - 要查詢其索引的值。

返回值

此函式返回該值的索引,如果未找到則返回 false。

示例 1

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

<?php
   $seq = new \Ds\Vector([1, 2, 3, 4, 5]);
   echo "The sequence elements are: \n";
   print_r($seq);
   $val = 3;
   echo "The given value is: ".$val;
   echo "\nAn index of value ".$val." is: ";
   #using find() function
   print_r($seq->find($val));
?>

輸出

執行上述程式後,將生成以下輸出:

The sequence elements are:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The given value is: 3
An index of value 3 is: 2

示例 2

如果在序列中未找到指定的值,則此函式將返回“false”。

以下是 PHP Ds\Sequence::find() 函式的另一個示例。我們使用此函式在此序列 (["Tutorials", "Point", "India"]) 中查詢指定值的索引:

<?php
   $seq = new \Ds\Vector(["Tutorials", "Point", "India"]);
   echo "The sequence elements are: \n";
   print_r($seq);
   $val = "Tutorix";
   echo "The given value is: ".$val;
   echo "\nAn index of value ".$val." is: ";
   #using find() function
   var_dump($seq->find($val));
?>

輸出

上述程式產生以下輸出:

The sequence elements are:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The given value is: Tutorix
An index of value Tutorix is: bool(false)
php_function_reference.htm
廣告