dongmacheng3222 2015-06-05 07:31
浏览 24
已采纳

如何修剪数组中每个键的最后两个字符?

This is my array (input):

$value = array("jan01" => "01", "feb02" => "02", "mar03" => "03", "apr04" => "04");

I am using this code to get the array keys:

implode(" ", array_map("ucwords", array_keys($value)));

Now my problem is I want to get all keys by triming the last two characters of each key.

How can I change/modify my code, so that it trim's the last two characters of each key?

EDIT:

I also want to skip first 3 keys, means I don't want the first 3 keys to be trimmed.

  • 写回答

2条回答 默认 最新

  • douzhi1919 2015-06-05 07:36
    关注

    I think this should work for you:

    Just take the substr() from your key and then use ucwords() on it.

    implode(" ",array_map(function($v){
        return ucwords(substr($v, 0, -2));
    },array_keys($value)));
    

    EDIT:

    AS from your updated question you don't want to take the substr from the first 3 elements. So just use a counter variable, e.g.

    $counter = 1;
    echo implode(" ", array_map(function($v)use(&$counter){
        if($counter++ > 3)
            return ucwords(substr($v, 0, -2));
        return ucwords($v);
    },array_keys($value)));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页