CKTIS / PHP
Laufzeit und Komplexität
Wiederholte lineare Suche prüft bis zu n Einträge pro Anfrage.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
$index = array_fill_keys($ids, true); isset($index[$id]);Wiederholte lineare Suche prüft bis zu n Einträge pro Anfrage. Ein Schlüsselindex braucht einen Durchlauf und Zusatzspeicher, danach kosten typische Hashzugriffe erwartet konstant viel. Schlüsseltypen vereinheitlichen, da PHP manche Zahlenstrings in int-Schlüssel umwandelt.
$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]
Beispiele
<?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"])]);
Ergebnis
[6,true,false]