php 如何在PHP中将整数转换为数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8118964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert an integer to an array in PHP?
提问by rob.s
What would be the most simple way to convert an integer to an array of numbers?
将整数转换为数字数组的最简单方法是什么?
Example:
例子:
2468
should result in array(2,4,6,8)
.
2468
应该导致array(2,4,6,8)
.
回答by Berry Langerak
回答by 4DA
You can cut-off the last digit by taking the number modulo 10.
您可以通过对数字取模 10 来截断最后一位数字。
Don't tell it to anyone!
不要告诉任何人!
do
{
$array.add(num % 10);
num = num / 10;
}
while (num != 0);
回答by Nathan Q
use str_split() function
使用 str_split() 函数
$array = str_split($str);
回答by breiti
Example #2 Splitting a string into component characters
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);