2016-10-01 43 views
0

我有一個語言文件看起來像這樣搜索和使用語言文件

$lang['dashboard ']='Dashboard'; 
$lang['financial_dashboard']='Financial Dashboard'; 
$lang['project_dashboard']='Project Dashboard'; 

陣列從數據庫

Array ([0] => Array ([id] => 109 [text] => dashboard [items] => Array ([0] => Array ([id] => 1 [text] => financial_dashboard [items] => 109) [1] => Array ([id] => 108 [text] => project_dashboard [items] => 109))) 

我如何才能找到結果和數組中替換值代替多維數組與我的語言文件中的值,但保持原樣,以便我的最終輸出看起來像這樣

Array ([0] => Array ([id] => 109 [text] => Dashboard [items] => Array ([0] => Array ([id] => 1 [text] => Financial Dashboard [items] => 109) [1] => Array ([id] => 108 [text] => Project Dashboard [items] => 109))) 

任何建議

回答

2

您可以簡單地遍歷數組,並檢查替換文本是否可用in_array()。如果找到,請將其替換爲相應的$ lang值。

$replace = array_keys($lang); // Text to replace i.e dashboard, financial_dashboard, project_dashboard 

foreach ($data as &$dt) { 
    if (is_array($dt['items'])) { 
    foreach ($dt['items'] as &$d) { 
     if (in_array($d['text'], $replace)) { 
      $d['text'] = $lang[$d['text']]; 
     } 
    } 
    } else { 
     if (in_array($dt['text'], $replace)) { 
     $dt['text'] = $lang[$dt['text']]; 
     } 
    } 
}