将数字集合转换为字符串到数字(Convert a collection of numbers as string to numbers)

编程入门 行业动态 更新时间:2024-10-26 00:18:41
数字集合转换为字符串到数字(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

更多推荐

本文发布于:2023-08-07 11:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464283.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   字符串   转换为   Convert   numbers

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!