Quote:
Originally Posted by callimuc
his one would just work at its best if you know/send the length of your numbers, while the one BlueMelon and me posted could be used as a function used by several scripts (like putting it into a class with custom functions)
Interesting to know
|
Starfire's method works with substring() too, for multiple numbers, by using temp.num.length() looping.
I disagree with everybody. I would use .charat()
PHP Code:
function numberToArray(num) {
for (temp.i = 0; i < num.length(); i++) {
temp.array.add(num.charat(i)); // like num.substring(i, 1) but more efficient
}
return(array);
}
So
temp.num = 426;
temp.test = numberToArray(num);
temp.test is {4, 2, 6}
You could also use charat() to most efficiently get the number at a single position, eg num.charat(1) would be 2.
EDIT: Someone beat me to charat. Darn. But looping backwards and using .insert is the same as using .add and looping forwards, Andrew.
EDIT2: I wonder if concatenation/tokenize would be more efficient than .add()? If so, temp.array = array SPC num.charat[i]; + tokenize it on the return.