Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Performance and complexity

Repeated linear membership scans can visit n entries per query.

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

Syntax

$index = array_fill_keys($ids, true); isset($index[$id]);

Repeated linear membership scans can visit n entries per query. Building a key index takes one pass and additional memory, then typical hash-table lookups have expected constant cost. Choose keys with consistent types because PHP normalizes certain numeric strings to integer keys.

 $ids=["A","B","C"]; $seen=0; foreach(["C","Z"] as $query){foreach($ids as $id){++$seen;if($id===$query)break;}}
$index=array_fill_keys($ids,true);echo json_encode([$seen,isset($index["C"]),isset($index["Z"])]);
[6,true,false]

Examples

<?php
$ids=["A","B","C"]; $seen=0; foreach(["C","Z"] as $query){foreach($ids as $id){++$seen;if($id===$query)break;}}
$index=array_fill_keys($ids,true);echo json_encode([$seen,isset($index["C"]),isset($index["Z"])]);

Result

[6,true,false]

Sources