我正在使用Laravel中的翻譯,並且我有幾個數組,其中包含映射到其對應翻譯的翻譯鍵。我需要刪除任何含有--
的翻譯,並將其替換爲空字符串。我已經在同一個函數中做了一些其他的str替換,所以我可能會打斷這個函數。當數值已被設置時,爲key數組賦值一個新值
這裏是整個函數的樣子:
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;
具體工作。
什麼不起作用?你有錯誤,還是分配不工作?你有什麼'$ out' var? – Gwendal
我沒有收到錯誤,只是沒有像我預期的那樣將破折號改爲空字符串。我發佈了以下答案中發生的事情。感謝您的詢問並嘗試提供幫助。 – kemosabe