如何将逗号分隔的数字字符串转换为整数数组? [英] How to convert a comma separated string of numbers to an array of integers?
本文介绍了如何将逗号分隔的数字字符串转换为整数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有字符串1,2,3,4,5
,我想将其转换为整数数组-最好的方法是什么?
Say I have the string 1,2,3,4,5
and I want to convert this to an array of integers - what would be the best way?
我知道我可以使用explode来创建带有字符串的数组,但是我需要将数组项设置为整数.
I know I can use explode to create an array with the string but I need the array items to be integers.
推荐答案
您可以使用 intval
爆炸字符串后,将其移至每个数组项:
You can use array_map
to apply intval
to each array item after you explode the string:
$string = "1,2,3,4,5";
$int_array = array_map("intval", explode(",", $string));
这篇关于如何将逗号分隔的数字字符串转换为整数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文