PHP循环数组并填写缺少的值
我有一个像这样的对象:(PHP循环数组并填写缺少的值,php,arrays,Php,Arrays,我有一个像这样的对象:($city) 所以基本上它是一个城市在多个地方的翻译 现在我的目标是用第一个可用的语言环境来填充缺少的语言环境 以下是我正在尝试的: $result = []; foreach (['fr', 'en', 'de'] as $locale) { if (is_string($city->$locale)) { $result[$locale] = $city->$locale; } } 但这也带来了: [ "fr
$city
)
所以基本上它是一个城市在多个地方的翻译
现在我的目标是用第一个可用的语言环境来填充缺少的语言环境
以下是我正在尝试的:
$result = [];
foreach (['fr', 'en', 'de'] as $locale) {
if (is_string($city->$locale)) {
$result[$locale] = $city->$locale;
}
}
但这也带来了:
[
"fr" => "Londres",
"en" => "London"
]
我的目标是:
[
"fr" => "Londres",
"en" => "London",
"de" => "Londres"
]
这里有一种相对有效的方法。处理第一个和/或多个
NULL
s。有关解释,请参见注释
<?php
class City
{
public ?string $de = NULL;
public ?string $fr = 'Londres';
public string $en = 'London';
}
$city = new City;
$result = [];
// Helper lookup array to keep track of all the NULL locales.
$nullIndices = [];
// Loop over each locale and fill the result.
foreach (['fr', 'de', 'en'] as $iso6391)
{
// Got a NULL value? Record its key so we can fill it
// once we have a first value.
if ($city->$iso6391 === NULL)
$nullIndices[] = $iso6391;
else
{
// Got an actual result (not a placeholder), store it now.
$result[$iso6391] = $city->$iso6391;
// Set a first value only once.
$firstValue = $firstValue??$result[$iso6391];
// For any accumulated NULL values, set them now to the first value.
// Remove the keys once they've been set to the placeholder.
foreach ($nullIndices as $nullIndex)
{
$result[$nullIndex] = $firstValue;
unset($nullIndices[$nullIndex]);
}
}
}
var_dump($result);
/*
array(3) {
["fr"]=>
string(7) "Londres"
["en"]=>
string(6) "London"
["de"]=>
string(7) "Londres"
}
*/
小心:您在'fr',en',de'
中漏掉了一个引号。在这种情况下,您的规则是如何将漏掉的德语翻译设置为法语?你只是选择了第一种语言?把名字从一种语言任意地放到另一种语言中似乎没有逻辑意义。这对任何人都有什么帮助?一旦你这样做了,你就会有一大堆不正确的翻译,而不是一大堆缺失的翻译。这a)仍然不是很有用,b)更难检测到。@那么michael可能会使用空字符串。或者,更好的是,调整你的排序算法以应对——这应该是可能的。为什么不呢?价值不重要。这听起来像是一个X-Y问题,而真正的问题可能是排序代码中的错误。(在最坏的情况下,如果出于某种奇怪的原因,空字符串或空字符串不是一个选项,那么最好使用“ZZZZ”之类的词)在它们中,不是一个来自不同语言的单词。然后,至少以后你仍然可以很容易地看到缺少翻译的地方。但实际上……只需修复你的排序算法,和/或找到一些真正的数据放在那里。)
<?php
class City
{
public ?string $de = NULL;
public ?string $fr = 'Londres';
public string $en = 'London';
}
$city = new City;
$result = [];
// Helper lookup array to keep track of all the NULL locales.
$nullIndices = [];
// Loop over each locale and fill the result.
foreach (['fr', 'de', 'en'] as $iso6391)
{
// Got a NULL value? Record its key so we can fill it
// once we have a first value.
if ($city->$iso6391 === NULL)
$nullIndices[] = $iso6391;
else
{
// Got an actual result (not a placeholder), store it now.
$result[$iso6391] = $city->$iso6391;
// Set a first value only once.
$firstValue = $firstValue??$result[$iso6391];
// For any accumulated NULL values, set them now to the first value.
// Remove the keys once they've been set to the placeholder.
foreach ($nullIndices as $nullIndex)
{
$result[$nullIndex] = $firstValue;
unset($nullIndices[$nullIndex]);
}
}
}
var_dump($result);
/*
array(3) {
["fr"]=>
string(7) "Londres"
["en"]=>
string(6) "London"
["de"]=>
string(7) "Londres"
}
*/