将数组值从字符串转换为int? [英] Convert array values from string to int?

查看:79
本文介绍了将数组值从字符串转换为int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:

$string = "1,2,3"
$ids = explode(',', $string);
var_dump($ids);

返回数组:

array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
}

我需要这些值的类型为 int ,而不是类型为 string .有没有比用 foreach 遍历数组并将每个 string 转换为 int 更好的方法了?

I need for the values to be of type int instead of type string. Is there a better way of doing this than looping through the array with foreach and converting each string to int?

推荐答案

您可以通过以下代码来实现,

You can achieve this by following code,

$integerIDs = array_map('intval', explode(',', $string));

这篇关于将数组值从字符串转换为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆