I'm trying to open an input file containing a variety of data type. Something like this:
Woof
50
Meow
30
//...
And I'm not quite sure how to do it. I've googled for ways to do it and I found something like ctype_digit
, is_digit
, and is_string
. I tested them out and I'm still not getting the expected result.
This is the code snippet I am using:
// Sorts the array by requested data type
function sortArray($anArray, $dataType){
$array_string = array();
$array_int = array();
foreach ($anArray as $element){
if (ctype_digit($element)){
array_push($array_int, $element);
}
elseif (ctype_alpha($element)){
array_push($array_string, $element);
}
}
if ($dataType == "int"){
return $array_int;
}
elseif ($dataType == "String"){
return $array_string;
}
} // end function sortArray($anArray, $dataType)
Can anyone help point out why it's not working?