I really looking for one function that can help me convert value like this (after var_dump):
string(2) "49" to integer or string(3) "1.2" to float.
this doesn't work for me Thanks
Here is my code: I simply have one Recursion function to check all elements of my array
foreach($arr as $key => $value)
{
if(is_array($value))
{
$this->__set_types($value);
}
if(is_numeric($value))
{
echo getcorrectvariable($value);
$arr[$key] = settype($value, gettype_fromstring($value));
var_dump($arr);
}
}
function gettype_fromstring($string){
// (c) José Moreira - Microdual (www.microdual.com)
return gettype(getcorrectvariable($string));
}
function getcorrectvariable($string){
// (c) José Moreira - Microdual (www.microdual.com)
// With the help of Svisstack (https://stackoverflow.com/users/283564/svisstack)
/* FUNCTION FLOW */
// *1. Remove unused spaces
// *2. Check if it is empty, if yes, return blank string
// *3. Check if it is numeric
// *4. If numeric, this may be a integer or double, must compare this values.
// *5. If string, try parse to bool.
// *6. If not, this is string.
$string=trim($string);
if(empty($string)) return "";
if(!preg_match("/[^0-9.]+/",$string)){
if(preg_match("/[.]+/",$string)){
return (double)$string;
}else{
return (int)$string;
}
}
if($string=="true") return true;
if($string=="false") return false;
return (string)$string;
}
Here is one pice of my array (var_dump)
array(3) {
[0]=>
array(3) {
[0]=>
array(9) {
["id"]=>
string(3) "106"
["idMessageType"]=>
string(1) "1"
["idClientSender"]=>
string(2) "59"
["idClientRecipient"]=>
string(2) "49"
["message"]=>
string(5) "rwert"
["creationTimeStamp"]=>
string(22) "2014-11-04 10:09:12+05"
["readingTimestamp"]=>
NULL
["status"]=>
string(1) "1"
["isMessageActive"]=>
string(1) "t"
}
[1]=>
array(9) {
["id"]=>
string(3) "105"
["idMessageType"]=>
string(1) "1"
["idClientSender"]=>
string(2) "59"
["idClientRecipient"]=>
string(2) "49"
["message"]=>
string(7) "qwrqewr"
["creationTimeStamp"]=>
string(22) "2014-11-04 10:08:56+05"
["readingTimestamp"]=>
NULL
["status"]=>
string(1) "1"
["isMessageActive"]=>
string(1) "t"
}
["client"]=>
array(4) {
["id"]=>
string(2) "49"
["name"]=>
string(12) "а аАаВаИаЛб"
["surname"]=>
string(14) "ааАбаИаПаОаВ"
["photoName"]=>
string(50) "49.jpg "
}
}
floatval or intval will do what you ask, separately.
If you want to retrieve the type... doesn´t make sense to me, but you could make a custom function using a regex... here:
function getTypedNumber($string)
{
$pattern = '/^(\d{1,3})((,)(\d{3}))*((\.)(\d{1,2}))?$|^(\d{1,3})((\.)(\d{3}))*((,)(\d{1,2}))?$/';
$replacement = '\1\8\4\11.\7\14';
$number = preg_replace($pattern, $replacement,$string);
if (is_numeric($number){
if (strpos($number, ".") === false ) return intval($number);
else return floatval($number);
}
else return false;
}
It will process number in the format 10.300,25 or 10,300.25 as well
EDIT
Forgot, that regex works assuming 2 decimal positions, it can be changed easily, if needed, but then it won´t process French/English Number style
Quick solution:
intval("42") // -> 42
intval("53.4") // -> 53
floatval("53.4") // -> 53.4
Why not googling "php parse float" first?