0

Is there a way to break up the value in an assigned variable without any blank spaces?

eg.

$var = 123456789;

i could do this with an explode array buy because there are no spaces im having difficulty.

$result = explode("", $var);

$results[0] = $a;
$result[1] = $b;
$result[2] = $c;

etc...

$a would = 1
$b would = 2
$c would = 3

etc...

Is this possible?

1
  • 1
    Don't forget to accept the answer that best fits your question. :) Commented Mar 19, 2011 at 4:30

5 Answers 5

5

This is what str_split is for. It will implicitly convert it's argument to a string:

$num = 12345;

$arr1 = str_split($num);

print_r($arr1);

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, use str_split. You can now split a string into individual characters.

Comments

1

Use str_split:

$var = 123456789;
$result = str_split($var);

Comments

0
$var = 123456789;
$length = strlen($var);

for ($i=0; $i<$length; $i++)  {
echo substr($var, $i, 1);
}

//echos each number

Comments

0
<?php 
function mySplit($in){
    $ret = array();
    $in = strval($in);
    $count = strlen($in);
    for($i =  0; $i < $count; $i++){
        $ret[] = substr($in,$i,1);
    }
    return $ret;
}
var_dump(explodeNumber(123));
echo "<br />";
var_dump(str_split(123));

array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" }

array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.