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条)

报告相同问题?

悬赏问题

  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致