HH\Pair::linearSearch

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

Description

public function linearSearch(
  Tu $search_value,
): int

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

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

Parameters

  • Tu $search_value - The value that will be search for in the current Pair.

Return Values

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

Examples

<?hh

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

$p = Pair {'foo', -1.5};

// Prints 0 (the index of the first value)
var_dump($p->linearSearch('foo'));

// Prints 1 (the index of the second value)
var_dump($p->linearSearch(-1.5));

// Prints -1 (the value doesn't exist in the Pair)
var_dump($p->linearSearch('bar'));
Output
int(0)
int(1)
int(-1)