[lang-ref] ( dict_reverse_lookup ) ( php )
<?php
public function testDictReverseLookup(): void
{
// array_flip
$d = [ 'A' => 1, 'B' => 2, 'C' => 3, 'D' => 2, 'E' => 1 ];
$reverseDict = array_flip($d);
$this->assertSame('C', $reverseDict[3]);
$this->assertSame('E', $reverseDict[1]); // last one wins
}
<?php
public function testDictReverseLookupAlternative(): void
{
// array_search($v, $d)
$d = [ 'A' => 1, 'B' => 2, 'C' => 3, 'D' => 2 ];
$key = array_search(2, $d, strict: true);
$this->assertSame('B', $key); // first one wins
$key = array_search(4, $d, strict: true);
$this->assertFalse($key); // not found: false is returned, not null
}