如何在PHP中将整数转换为数组? [英] How to convert an integer to an array in PHP?

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

问题描述

将整数转换为数字数组的最简单方法是什么?

What would be the most simple way to convert an integer to an array of numbers?

示例:

2468 应该导致 array(2,4,6,8).

推荐答案

您可以使用 str_splitintval:

You can use str_split and intval:

$number = 2468;

    $array  = array_map('intval', str_split($number));

var_dump($array);

这将给出以下输出:

array(4) {
  [0] => int(2)
  [1] => int(4)
  [2] => int(6)
  [3] => int(8)
}

演示

这篇关于如何在PHP中将整数转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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