Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Linear search

Scan values in iteration order until the first equal item.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

array_search($needle, $haystack, true);

Scan values in iteration order until the first equal item. Strict comparison keeps the integer 0 distinct from the string "0". array_search returns the matching key, which may itself be 0, or false on failure. A scan examines up to n items without requiring sorting.

$items=[0=>"0",7=>0,9=>"0"];echo json_encode([array_search("0",$items,true),array_search(0,$items,true),array_search("x",$items,true)===false]);
[0,7,true]

Examples

<?php
$items=[0=>"0",7=>0,9=>"0"];echo json_encode([array_search("0",$items,true),array_search(0,$items,true),array_search("x",$items,true)===false]);

Result

[0,7,true]

Sources