Home > Article > Backend Development > How to convert each digit of a number into an element of an array in php
Conversion method: 1. Use the "strval($num)" statement to convert the specified number into a string; 2. Use the "str_split($str)" statement to split the string into an array. String A numeric character corresponds to an element of the array.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php will convert each digit of the number The value is converted into an element of the array
We can use strings and the str_split() function; str_split() can split the string into an array, and one character of the string corresponds to one element of the array.
First convert the number into a string
Use the str_split() function to split the string, so that the value of each digit of the number corresponds to the array of an element.
Implementation code:
<?php $num=12365; $str=strval($num); $arr=str_split($str); var_dump($arr); ?>
Output result:
Recommended learning: "PHP video tutorial 》
The above is the detailed content of How to convert each digit of a number into an element of an array in php. For more information, please follow other related articles on the PHP Chinese website!