背景:我正在遍历数据库数据并将其输出到表中的页面。我的循环首先输出标头信息,但标头就是我所说的未准备好前端的信息。
例如:child_0_birthday需要更改为Child 1 Birthday。
到目前为止,我的财产是:
//Define table header section here
foreach ($fields as $k => $v){
$html .= '<th class="manage-column" data-key="'.esc_html($v).'">'.updateHeaderName(vsz_cf7_admin_get_field_name($v)) .'</th>';
}
//This function would do the clean up
$devHeaderNames = array("child_0_birthday", "child_0_fname", "child_0_lname", "child_0_age");
function updateHeaderName($oldHeader) {
if (in_array($oldHeader, $devHeaderNames))
{
//This is where I would want to change the value of $oldHeader to something cleaner and spit it back out. I could write a very long if statement but there has to be a better way.
if ($oldHeader == child_0_fname){
$newHeader = "Child 1 First Name";
return $newHeader;
}else if...
}
}
问题:是否有一种更有效的方法,然后编写一个很长的if语句来更改显示在每列标题中的字符串?
答案 0 :(得分:1)
您可以只使用数组来转换键和值的关联。假设$header = "child_0_birthday"
:
$devHeaderNames = array("child_0_birthday" => "Child 1 Birthday"); //etc...
if(isset($devHeaderNames[$header])) {
$header = $devHeaderNames[$header];
}