I got sec which is dynamic
$seconds= 23456;
i want to assign a variable for each digit
any help would be greatly appreciated
I got sec which is dynamic
$seconds= 23456;
i want to assign a variable for each digit
any help would be greatly appreciated
Hi Ben
Here is a little script below that will do what you asked:
[php]<?php
$seconds= 23456;
$digits = strlen($seconds); // count how many digits there are.
$x = 0; // set $x at 0 as we want to start with the first digit
for($i=0; $i<=$digits-1; $i++) // loop through each digit
{
$s[] = substr($seconds, $x, 1); // set the digit into array.
$x++; // move along to the next digit.
}
// to output each digit call it from the array like so:
echo $s[0]; // outputs: 2
echo $s[1]; // outputs: 3
echo $s[2]; // outputs: 4
echo $s[3]; // outputs: 5
echo $s[4]; // outputs: 6
?>[/php]
this will work regardless of how many digits there are whether it be 1 or 100.
Hi there,
Redscouse gave a good answer, however it may be simpler to use the str_split function:
[php]$string = “123456”;
$array = str_split($string);
[/php]
$array then returns:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
Just note PHP will automatically typecast 12345 in to “12345” when referenced as a string, so str_split will work any time.
thank you for your responds, both ways work fine, however I used str_split since it is one lie code
thanks again
Ben