Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
vector

What is the DS\Vector find() method in PHP?

Talha Ashar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The find() method of the DS\Vector class in PHP finds the index of the vector at which a particular value is stored.

The process is illustrated below:

The prototype of the find() method is shown below:

mixed public Ds\Vector::find( mixed $value ) 

Parameters

The find() method takes a single mandatory parameter, i.e., the value whose index is being searched for. This value can have any data type.

Return value

If the provided value exists in the vector, then the find() method returns the index of the value; otherwise, it returns false.

Example

The code below shows how the find() method works in PHP:

<?php
// initialize vector
$newVector = new \Ds\Vector([1, "abc", 10, 2, "efg"]);
print_r($newVector);
// define values to be searched for
$valuesToSearch = ["abc", 2, "2", 5];
for ($i = 0; $i < 4; $i++)
{
// find index of each value
$index = $newVector->find($valuesToSearch[$i]);
if($index)
{
print("The value " . $valuesToSearch[$i] . " was found at index " . $index . ".\n");
}
else
{
print("The value " . $valuesToSearch[$i] . " does not exist in the vector.\n");
}
}
?>

The code above produces the following output:

Ds\Vector Object
(
[0] => 1
[1] => abc
[2] => 10
[3] => 2
[4] => efg
)
The value abc was found at index 1.
The value 2 was found at index 3.
The value 2 does not exist in the vector.
The value 5 does not exist in the vector.

Note: To run this program, you need to install Data Structures for PHP on your local machine.

Explanation

The vector newVector is initialized with five distinct values of different data types. The array valuesToSearch contains a list of values that will be searched for in the vector.

A for-loop proceeds to iterate over each value in the valuesToSearch array. The find() method in line 13 is invoked during each loop iteration.

Since the first two values in valuesToSearch, "abc" and 2, exist in newVector, the find() method returns the indices of both these values, i.e., 1 and 3 respectively.

In the third iteration of the for-loop, the find() method searches newVector for the value "2". Although a value of 2 does exist in newVector, there is a mismatch of data types as the value being searched for is a string, whereas the value in the vector is an integer. Consequently, the find() method returns false.

Finally, since the last value, 5, does not exist in newVector, the find() method returns false.

RELATED TAGS

php
vector

CONTRIBUTOR

Talha Ashar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring