How to Convert Number to Character Array in PHP ? Last Updated : 20 Mar, 2024 Comments Improve Suggest changes Like Article Like Report Given a number, the task is to convert numbers to character arrays in PHP. It is a common operation when you need to manipulate or access individual digits of a number. This can be particularly useful in situations where you need to perform operations on the digits of a number, such as digital root calculations, digit summing, or validations. Below are the approaches to convert numbers to character arrays in PHP: Table of Content Using str_split() FunctionUsing Manual Conversion with LoopUsing preg_split() FunctionUsing str_split() FunctionThe str_split() function is one of the simplest ways to convert a string into an array of characters. To use it with numbers, you first need to convert the number to a string and then use str_split() to split the string into array of characters. Example: This example shows the use of the above-explained approach. PHP <?php $number = 12345; // Convert number to string and // then to character array $charArray = str_split((string)$number); print_r($charArray); ?> OutputArray ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Using Manual Conversion with LoopA number can be converted to a character array by iterating over each digit. This method gives you more control over the conversion process. First, we convert the integer into a string and then we use for loop to iterate it. Example: This example shows the use of the above-explained approach. PHP <?php $number = 12345; $numberStr = (string)$number; $charArray = []; // Loop through each character of the string for($i = 0; $i < strlen($numberStr); $i++) { // Add each digit to the array $charArray[] = $numberStr[$i]; } print_r($charArray); ?> OutputArray ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Using preg_split() FunctionThe preg_split() function is primarily used for splitting strings based on a regular expression pattern. ' // ': This empty regular expression pattern matches between each character in the string, effectively splitting it into individual characters.' -1 ': Indicates that there is no limit to the number of splits that can be performed.PREG_SPLIT_NO_EMPTY: This flag tells preg_split() not to return empty matches. In this case, it ensures that consecutive digits are treated as separate elements in the resulting array.Example: This example shows the use of the above-explained approach. PHP <?php $number = 12345; $numberAsString = (string) $number; // preg_split() splits the string according to the regular expression pattern $charArray = preg_split('//', $numberAsString, -1, PREG_SPLIT_NO_EMPTY); print_r($charArray); ?> OutputArray ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Comment More infoAdvertise with us Next Article How to Convert Number to Character Array in PHP ? A ashokjaiswal Follow Improve Article Tags : PHP Similar Reads How to convert a String into Number in PHP ? Strings in PHP can be converted to numbers (float/ int/ double) very easily. In most use cases, it won't be required since PHP does implicit type conversion. This article covers all the different approaches for converting a string into a number in PHP, along with their basic illustrations.There are 4 min read How to Convert a Given Number to Words in PHP? Given an integer N, your task is to convert the given number into words using PHP.Examples:Input: N = 958237764Output: Nine Hundred Fifty Eight Million Two Hundred Thirty Seven Thousand Seven Hundred Sixty FourInput: N = 5000Output: Five ThousandBelow are the approaches to convert a given number to 5 min read How to extract Numbers From a String in PHP ? Extracting numbers from a string involves identifying and isolating numerical values embedded within a text. This process can be done using programming techniques, such as regular expressions, to filter out and retrieve only the digits from the string, ignoring all other characters.Here we have some 3 min read How to convert an Integer Into a String in PHP ? The PHP strval() function is used to convert an Integer Into a String in PHP. There are many other methods to convert an integer into a string. In this article, we will learn many methods.Table of ContentUsing strval() function.Using Inline variable parsing.Using Explicit Casting.Using sprintf() Fun 3 min read How to get total number of elements used in array in PHP ? In this article, we will discuss how to get total number of elements in PHP from an array. We can get total number of elements in an array by using count() and sizeof() functions. Using count() Function: The count() function is used to get the total number of elements in an array. Syntax: count(arra 2 min read How to read each character of a string in PHP ? A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. Here are some approaches to read each character of a string in PHPTable of ContentUsing str_split() method - The str_ 4 min read How to find Sum the Digits of a given Number in PHP ? We will explore how to find the sum of the digits of a given number in PHP. This task involves extracting each digit from the number and adding them together to get the final sum. Table of Content Using a loop to extract digits one by oneUsing mathematical operations to extract digitsUsing a loop to 2 min read How to format Phone Numbers in PHP ? In this article, we will learn how to format phone numbers using PHP. When we need to store a phone number then we store the formatting phone number. Using PHP, we can easily format phone numbers. Approach: In this article, we will format the phone number using the preg_match() method. We can use th 1 min read How to convert String to Float in PHP ? Converting a string to a float in PHP is a common requirement when handling numeric data stored in string format. This conversion allows the numeric string to be used in calculations or comparisons, ensuring accurate manipulation of data within the program.There are many methods to convert a string 3 min read Like