I followed the leads in the questions this and this.
I am trying to convert an input stream of numbers to an array of integers. The code should be self explanatory.
$handle = fopen("php://stdin","r");
print("Enter space separated numbers to be made to an array\n");
$numStream = fgets($handle);
print("Creating array from : {$numStream}\n");
//Using explode to create arrays
//now we have an array of Strings
$numArray = explode(" ", $numStream);
var_dump($numArray);
print(getType($numArray[0]));
print("\n");
array_walk($numArray, 'intval');
print(getType($numArray[1]));
print("\n");
var_dump($numArray);
print_r($numArray);
I am trying to convert the String array,
array_walk($numArray, 'intval')
The last two print blocks prints the type of an array element before and after conversion.
The output is string in both the cases
string
string
I wonder what is going on here? Possibly..
- The conversion is wrong
- How the type is checked is wrong
Or possibly both.
Adding the complete input and output,
$ php arrays/arrayStringToInteger.php
Enter space separated numbers to be made to an array
1 1
Creating array from : 1 1
Array
(
[0] => 1
[1] => 1
)
string
string
/home/ubuntu/workspace/basics/arrays/arrayStringToInteger.php:22:
array(2) {
[0] =>
string(1) "1"
[1] =>
string(2) "1
"
}
Array
(
[0] => 1
[1] => 1
)