doupeng3501 2016-06-24 18:49
浏览 63
已采纳

PHP - 在list()参数中将字符串转换为int?

I am refining my code and I have a couple of spots where I need to cast strings as integer but the limitations of the list() function are dissuading me from using list(). Specific example:

$x = '12-31-2010';

$explode = explode("-", $x);

// Need to do the following because e.g. echo gettype($explode[0]) ---> string

$month = (int)$explode[0];
$day   = (int)$explode[1];
$year  = (int)$explode[2];

What I would like to do (but get a fatal error) to make things a little more tidy:

list((int)$month, (int)$day, (int)$year) = explode("-", $x); // I want echo(gettype) ---> integer for each variable

Is there any way to do this or is the best I can do the following?

list($month, $day, $year) = explode("-", $x);

$month = (int)$month;
$day   = (int)$day;
$year  = (int)$year;
  • 写回答

2条回答 默认 最新

  • doulu8415 2016-06-24 18:54
    关注

    By this reference:-

    how to convert array values from string to int?

    you can do it like below:-

    list($month, $day, $year) = array_map('intval', explode('-', $x));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里