PHP - Ds Sequence::find() Function
The PHP Ds\Sequence::find() function is used to find an index of the specified value in a sequence. This function returns an index of the given value, or 'false' if the value is not found in the sequence.
An index is the position of an element in a sequence, starting at "0", the index "0" represents the first element, "1" represents the second element, and so on.
Syntax
Following is the syntax of the PHP Ds\Sequence::find() function −
public abstract mixed Ds\Sequence::find( mixed $value )
Parameters
Following is the parameter of this function −
- value − The value of that index is to be found.
Return value
This function returns an index of the value, or false if not found.
Example 1
The following is the basic example of the PHP Ds\Sequence::find() function −
<?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)); ?>
Output
Once the above program is executed, it generates the following output −
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
Example 2
If the specified value is not found in a sequence, this function will return 'false'.
Following is another example of the PHP Ds\Sequence::find() function. We use this function to find an index of the specified value in this sequence (["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)); ?>
Output
The above program produces the following output −
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)