如何以数组或任何其他简短方法获得每个月的第一个角色资本来实现这一目标?
$heading = array("apr","may","jun","jul","aug","sep","oct","nov","dec","jan","feb","mar");
foreach($heading as $key=> $month)
{
echo $heading[$key] = ucfirst($month); //Apr, May,Jun It's ok
}
print_r($heading);
当前输出
Array
(
[0] => apr
[1] => may
[2] => jun
[3] => jul
[4] => aug
[5] => sep
[6] => oct
[7] => nov
[8] => dec
[9] => jan
[10] => feb
[11] => mar
)
预期输出
Array
(
[0] => Apr
[1] => May
[2] => Jun
[3] => Jul
[4] => Aug
[5] => Sep
[6] => Oct
[7] => Nov
[8] => Dec
[9] => Jan
[10] => Feb
[11] => Mar
)
如果我打印echo $heading[$key] = ucfirst($month);
它显示OK但是还有其他任何捷径方法吗?
答案 0 :(得分:2)
这应该适合你:
<?php
$heading = array("apr","may","jun","jul","aug","sep","oct","nov","dec","jan","feb","mar");
array_walk(
$heading,
function(&$a) {
$a = ucfirst($a);
}
);
print_r($heading);
?>
答案 1 :(得分:1)
更新原始数组,而不是输出值。
$heading = array("apr","may","jun","jul","aug","sep","oct","nov","dec","jan","feb","mar");
foreach($heading as $key=> $month)
{
$heading[$key] = ucfirst($month); //Apr, May,Jun It's ok
}
print_r($heading);
此外,您在变量名称中输入了拼写错误,这可能会导致您遇到麻烦。 (确保激活所有形式的错误报告,以便捕获这些内容)
答案 2 :(得分:1)
它可以在一行中完成:
$heading = array_map('ucfirst', $heading);