3v4l.org

run code in 300+ PHP versions simultaneously
<?php $allLanguages = ['en', 'es', 'fr']; $values = ['es' => 'Spanish1']; $filesContent = [ 'foo' => [ 'es' => ['bar' => 'Spanish2'], 'fr' => ['bar' => 'French'] ] ]; $fileName = 'foo'; $key = 'bar'; $original = []; foreach ($allLanguages as $languageKey) { $original[$languageKey] = $values[$languageKey] ?? $filesContent[$fileName][$languageKey][$key] ?? ''; } var_export($original);
Output for 7.0.16, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.26, 8.1.0 - 8.1.13, 8.2rc3
array ( 'en' => '', 'es' => 'Spanish1', 'fr' => 'French', )

preferences:
60.37 ms | 409 KiB | 5 Q