HH\Vector::linearSearch

Returns the index of the first element that matches the search value

Description

public function linearSearch<Tu super Tv>(
  Tu $search_value,
): int

If no element matches the search value, this function returns -1.

Parameters

  • $search_value - The value that will be searched for in the current Vector.

Return Values

  • int - The key (index) where that value is found; -1 if it is not found.

Examples

<?hh

namespace Hack\UserDocumentation\API\Examples\Vector\LinearSearch;

$v = Vector {'red', 'green', 'blue', 'yellow'};

// Prints 2
var_dump($v->linearSearch('blue'));

// Prints -1 (since 'purple' is not in the Vector)
var_dump($v->linearSearch('purple'));
Output
int(2)
int(-1)