2016-10-01 44 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']]; 
     } 
    } 
}