A storage manifest can put the first rack in slot zero and an overflow rack under a string key. PHP’s array_search returns the matched key, not a boolean success flag. Code that uses if (!$key) or $key ?: null misclassifies slot zero and an empty string key as misses. A separate strictness setting controls how the values themselves are compared.
Run the manifest example
Use a supported PHP 8 series CLI such as PHP 8.3. Save as slot.php. Run php -l slot.php to check the syntax, then php slot.php to run the behavior checks. There are no dependencies beyond PHP.
<?php
declare(strict_types=1);
function locateSlot(array $slots, string $requested): int|string|null
{
$key = array_search($requested, $slots, true);
return $key === false ? null : $key;
}
$slots = [0 => 'rack-7', 'overflow' => 'rack-9', 2 => 42,
5 => '42', 6 => 'rack-7', '' => 'spare'];
foreach (['rack-7' => 0, 'rack-9' => 'overflow', 'spare' => '',
'unknown' => null] as $requested => $expected) {
if (locateSlot($slots, $requested) !== $expected) {
throw new LogicException('Wrong slot for ' . $requested);
}
}
if (locateSlot($slots, '42') !== 5 || locateSlot([], 'rack-7') !== null) {
throw new LogicException('Type or empty-array check failed');
}
printf("rack-7 is in slot %d\n", locateSlot($slots, 'rack-7'));Expected output from the program
rack-7 is in slot 0The third argument to array_search selects strict comparison. It prevents the string identifier '42' from selecting the earlier integer value 42. The check $key === false does a different job. It distinguishes the documented miss sentinel from valid keys. The wrapper translates that sentinel to null so callers can use $slot === null. A file-level strict_types declaration does not set the search function’s comparison mode for you.
Choose what duplicates mean
The function returns the first match. In the fixture, slot six also contains rack-7, but slot zero wins because it appears first in the array. If duplicates are invalid in your manifest, validate them at import time instead of assuming a successful search proves uniqueness. If duplicates are intentional and every slot is needed, use a strict search that returns all matching keys, such as array_keys($slots, $requested, true).
If a migration still loses zero, inspect the next caller or template for another truthiness check. If a value fails to match, inspect the imported types and casing before disabling strict comparison. Normalizing externally supplied IDs can be reasonable, but do it deliberately when ingesting them so searches and duplicate checks use the same definition.
References and related reading: PHP array_search documentation and Java Samples PHP tutorials.