array_search

array_search(mixed $needle, array $haystack, bool $strict = false): int|string|false

Searches an array for a value and returns its key, or false when not found.

C C23

C23
#include <stdio.h>

int main(void) {
    int xs[] = {1, 2, 3};
    int found = -1;
    for (int i = 0; i < 3; i++) {
        if (xs[i] == 2) {
            found = i;
            break;
        }
    }
    printf("%d\n", found);
    return 0;
}
Output
1

C++ C++26

C++26
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> xs{1, 2, 3};
    auto it = std::find(xs.begin(), xs.end(), 2);
    std::cout << (it == xs.end() ? -1 : it - xs.begin()) << "\n";
    return 0;
}
Output
1

C# v11.0.100

v11.0.100
var xs = new[] { 1, 2, 3 };
Console.WriteLine(Array.IndexOf(xs, 2));
Console.WriteLine(Array.IndexOf(xs, 9));
Output
1
-1

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"slices"
)

func main() {
	xs := []int{1, 2, 3}
	fmt.Println(slices.Index(xs, 2), slices.Index(xs, 9))
}
Output
1 -1

Java v27.0.1

v27.0.1
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> xs = List.of(1, 2, 3);
        System.out.println(xs.indexOf(2));
        System.out.println(xs.indexOf(9));
    }
}
Output
1
-1

JavaScript v26.13.1

v26.13.1
const xs = [1, 2, 3];
console.log(xs.indexOf(2), xs.indexOf(9), xs.findIndex((v) => v > 2));
Output
1 -1 2

PHP v8.6.0

v8.6.0
$xs = ["a" => 1, "b" => "2", "c" => 3];
echo json_encode([array_search(2, $xs), array_search("2", $xs, true), array_search(9, $xs)]);
Output
["b","b",false]

Python v3.15.2

v3.15.2
xs = [1, 2, 3]
print(xs.index(2))
print(xs.index(3) if 3 in xs else "missing")
Output
1
2

Rust v1.99.1

v1.99.1
fn main() {
    let xs = [1, 2, 3];
    println!("{:?}", xs.iter().position(|&v| v == 2));
    println!("{:?}", xs.iter().position(|&v| v == 9));
}
Output
Some(1)
None

TypeScript v26.13.1

v26.13.1
const xs: number[] = [1, 2, 3];
console.log(xs.indexOf(2), xs.indexOf(9), xs.findIndex((v) => v > 2));
Output
1 -1 2

Language differences

C: hand-written linear search (example); the standard bsearch is binary-only and requires pre-sorting.

C++: std::find returns an iterator; after the == end() check you can take the pointer difference (example); std::find_if is the predicate counterpart.

C#: Array.IndexOf/List<T>.IndexOf return -1 when absent (example); predicate version is FindIndex.

Go: slices.Index works directly since 1.21 (example); before that a hand-written loop; -1 when not found.

Java: List.indexOf returns the index or -1 (example); for arrays use Arrays or a loop; predicate version via stream filter + findFirst.

JavaScript: indexOf uses strict equality (===), so reference types compare identity; the predicate version is findIndex (third item in the example); -1 when not found.

PHP: the return is a key, not a position (the example returns "b"); comparison is loose by default — array_search(2, ...) matches the string "2" (first item in the example), so with string haystacks always pass $strict = true; not-found is false, so test with !== false (a returned index 0 would be misjudged as falsy).

Python: list.index raises ValueError when absent (utterly different from the -1/false/None styles elsewhere) — the idiom is an in pre-check (second line of the example); the first matching index is returned.

Rust: iter().position(...) returns Option<usize> — Some(index)/None wipes out the whole class of 'forgot to handle not-found' bugs via the type system (example).

TypeScript: same indexOf/findIndex as JavaScript (example).