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 "
}
}