本文介绍多种高效方式,利用 php 原生函数或 laravel collections,在嵌套数组中根据子数组内的值(如 iso 3166-1 alpha-2 国家代码)反向检索顶层键(如货币代码),避免手动 forea
ch 循环,兼顾性能与可读性。
在实际开发中,我们常遇到类似这样的结构:以货币代码为键,其值为该货币流通国家的 ISO 代码数组。当用户传入一个国家代码(如 'AT'),我们需要快速定位它所属的货币(如 'EUR')。虽然 foreach 可行,但 PHP 提供了更简洁、函数式且可复用的方案。
核心思路是:遍历 $currencies 的每个子数组,判断目标国家代码是否存在于其中,并返回匹配的键。注意——array_filter() 默认保留键名,因此后续用 array_keys() 即可提取结果:
$currencies = [
'EUR' => ['AT', 'BE', 'CY', 'EE', 'FI', 'FR', 'DE', 'GR', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PT', 'ES', 'SI', 'SK'],
'JPY' => ['JP'],
'IDR' => ['ID']
];
$country = 'AT';
// 使用 array_flip 提升查找效率(O(1) 哈希查找 vs O(n) 线性扫描)
$filtered = array_filter($currencies, function ($countriesList) use ($country) {
return isset(array_flip($countriesList)[$country]);
});
$currency = array_keys($filtered)[0] ?? null; // 获取第一个匹配货币,未找到则为 null
var_dump($currency); // string(3) "EUR"⚠️ 注意:array_flip() 在子数组较大时内存开销略高,但对国家列表(通常 ≤ 50 项)完全无压力;若追求极致零内存分配,仍可用 in_array($country, $countriesList, true),但性能略低。
借助 Collections 链式调用,代码更声明式、易读:
use Illuminate\Support\Collection;
$res = collect($currencies)
->filter(fn($list) => in_array($country, $list, true))
->keys()
->first(); // 直接取首个键,避免 array_keys()[0]
// 或更高效版(同上使用 array_flip)
$res = collect($currencies)
->filter(fn($list) => isset(array_flip($list)[$country]))
->keys()
->first();✅ ->first() 比 ->all()[0] 更安全(空集合时返回 null 而非报错),也更语义化。
为提升工程化程度,建议封装为独立函数,支持多结果或默认值:
function findCurrencyByCountry(array $currencies, string $country, bool $firstOnly = true): array|string|null {
$matches = array_filter($currencies, function ($list) use ($country) {
return in_array($country, $list, true);
});
if ($firstOnly) {
return array_keys($matches)[0] ?? null;
}
return array_keys($matches); // 返回所有匹配货币(如某国支持多币种)
}
// 使用示例
echo findCurrencyByCountry($currencies, 'JP'); // "JPY"
print_r(findCurrencyByCountry($currencies, 'XX', false)); // []这一模式同样适用于其他“值→键”反查场景,如语言代码映射、时区区域归属等,掌握后可显著提升数组操作效率。