gmol
gmol

Reputation: 25

Store each number in array

Hey guys I'm new in php and I'm trying to store the numbers from a input into a string, something like this.

let's say that the input stores the number 1234:

$input = 1234;

So I wan't to store a single digit and not the whole number in the array like:

$arr[0] = 1;
$arr[1] = 2;
$arr[2] = 3;
...

How can I do that?

Upvotes: 0

Views: 584

Answers (4)

Hans
Hans

Reputation: 3523

$input = (string)1234;
echo $input[2];

echo will output 3 -- that is the spot in the string/pseudo array represented by the array index 2

Upvotes: 1

adidasadida
adidasadida

Reputation: 120

Maybe type casting would work. See below for sample code.

$input = 1234;
$arr = (string)$input;

for($i = 0; $i < strlen($arr); $i++) {
  echo $arr[$i];
}

Upvotes: 3

Mark Baker
Mark Baker

Reputation: 212452

$str = "1234";
$array = str_split($str);
var_dump($array);

Upvotes: 1

Joel
Joel

Reputation: 577

http://www.php.net/manual/en/function.str-split.php

String split

$arr = str_split($input);

Upvotes: 2

Related Questions