How to convert an integer to an array in PHP?
You can use str_split
and intval
:
$number = 2468;
$array = array_map('intval', str_split($number));
var_dump($array);
Which will give the following output:
array(4) {
[0] => int(2)
[1] => int(4)
[2] => int(6)
[3] => int(8)
}
Demo
How to create integer out of array?
Simple use implode
as
$array = array(7,4,7,2);
echo (int)implode("",$array);// 7472
Convert a comma-delimited string into array of integers?
You can achieve this by following code,
$integerIDs = array_map('intval', explode(',', $string));
Converting array of string to array of integers in PHP
You should use array_map
instead of array_walk
:
$numArray = array_map('intval', $numArray);
If you still want to use array_walk
- refer to a manual which says:
If callback needs to be working with the actual values of the array, specify the first parameter of callback as a reference. Then, any changes made to those elements will be made in the original array itself.
As intval
function doesn't work with references you need to wrap it in some other logics, something like:
array_walk($numArray, function(&$v){ $v = intval($v); });
// which is the same as @BizzyBob solution)))
Converting integer to array of strings
Simply use the string split function and reverse it.
$num = 321;
$array = str_split(strrev($num));
Array
(
[0] => 1
[1] => 2
[2] => 3
)
How to Convert Range of Integers into PHP Array
simple explode call:
$array=explode(',',$groups);
How to cast variable to array
You can cast a variable to an array by using:
$var = (array)$arr;
convert string array to integer array in php
You can use array_map
$TaxIds = array_map(function($value) {
return intval($value);
}, $TaxIds);
Related Topics
Converting a Byte Array into an Image Using PHP and HTML
Regular Expression: Find Range Except for One Letter or a Range
Fatal Error: Uncaught Argumentcounterror: Too Few Arguments to Function
How to Add a Condition Inside a PHP Array
SQL Query with Binary Data (PHP and MySQL)
How to Sftp Upload Files from PHP
PHP MySQL - When Is the Best Time to Disconnect from the Database
How to Render Content from String/Database with Twig
Strtolower() for Unicode/Multibyte Strings