将数字集合转换为字符串到数字(Convert a collection of numbers as string to numbers)
如果我有一个字符串: 10 20 3 4 15 6
如何将其转换为单个数字并将其存储在数组中?
If I have a string as : 10 20 3 4 15 6
How can I convert it to individual numbers and store it in a array?
最满意答案
在处理变量类型时,PHP非常聪明。 你不需要它是一个整数,它可以是一串数字,PHP在对它们执行操作时仍会将其视为整数。
如果你想让每个元素都是以空格分隔的数字,你就可以了
$array = explode(" ", "10 20 3 4 15 6");那么$array的输出就是
Array ( [0] => 10 [1] => 20 [2] => 3 [3] => 4 [4] => 15 [6] => 6 )现场演示
PHP is very clever when dealing with types of variables. You don't need it to be an integer, it can be a string of numbers, and PHP would still treat it as integers when performing operations on them.
If you want to have each element be the numbers separated by spaces, you simply do
$array = explode(" ", "10 20 3 4 15 6");The output of $array would then be
Array ( [0] => 10 [1] => 20 [2] => 3 [3] => 4 [4] => 15 [6] => 6 )Live demo
更多推荐
发布评论