当已设置值时,为键=>值数组分配新值


Assigning a new value to array of key=>value when the value has already been set

我正在Laravel中使用翻译,我有几个带有翻译键的数组,这些数组映射到相应的翻译。我需要删除任何具有--的翻译并将其替换为空字符串。我已经在同一个函数中做了一些其他的str替换,所以也许我正在破坏这个函数。

整个函数如下所示:

public function getWithContext($locale, $context)
{
    $this->registerContext($context);
    $contextKey = "{$locale}.{$context}";
    if ($this->has($contextKey)) return $this->get($contextKey);
    $out = [];
    $data = $this->get($locale);
    foreach ($data as $key => $value) {
        if (preg_match("/^({$context}'.)/", $key)) {
            $k       = str_replace("{$context}.", "", $key); 
            $out[$k] = ($value == "—-") ? "" : $value;
        }
    }
    if (!empty($out)) {
        $this->put($contextKey, new Collection($out));
    }
    return $out;
}

我正在尝试让线路$out[$k] = ($value == "--") ? "" : $value;专门工作。

不确定这是否与此有关,但在您的问题中您提到了字符串"--"但在您的代码中,字符串"—-"它的第一个破折号具有不同的字符

我找到了解决问题的方法。通过键盘输入的破折号的编码与服务器看到的编码不同。因此,我不得不将值从服务器复制到代码中,并且效果很好。