Home  >  Article  >  Backend Development  >  How to convert each digit of a number into an element of an array in php

How to convert each digit of a number into an element of an array in php

青灯夜游
青灯夜游Original
2021-09-23 15:25:261917browse

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.

How to convert each digit of a number into an element of an array in php

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:

How to convert each digit of a number into an element of an array in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn