Home > Backend Development > PHP Problem > 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

青灯夜游
Release: 2023-03-12 21:12:02
Original
1994 people have browsed it

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);
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template