CluelessNoob
CluelessNoob

Reputation: 696

How to convert an array into a number in PHP?

I want to convert an array in one large number in PHP.

For example, I have an array $array:

$array[0] = 10;
$array[1] = 20;
$array[2] = 30;
$array[3] = 40;

I want this to be:

$one_large_number = 10203040;

I read somewhere the way to convert an array into a string, but that won't allow me to perform mathematical operations, right?

So anyone know how to convert the array to one continuous number?

Thanks.

Upvotes: 3

Views: 409

Answers (4)

sprain
sprain

Reputation: 7672

To make sure you'll get a number:

intval(implode('', $array));

Upvotes: 1

Vahan
Vahan

Reputation: 534

Hi you can use implode it's an alias of join

$array[0] = 10;
$array[1] = 20;
$array[2] = 30;
$array[3] = 40;
$one_large_number = implode('',$array);

Upvotes: 1

Vishal
Vishal

Reputation: 2181

Try implode() function as:

$array = array(10, 20, 30, 40);
$one_large_number = implode("", $array);
// Output: 10203040

Upvotes: 1

Andrius Naruševičius
Andrius Naruševičius

Reputation: 8578

join("", $array);

A little more about it in here: http://www.w3schools.com/php/func_string_join.asp

http://codepad.org/Dv0zdtaJ - live example. As you can see, you can easily perform further mathematical functions with that number :)

Upvotes: 1

Related Questions