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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测