This function might be useful when you want to convert integers or floats to strings. It works fine with multidimensional arrays and keeps key => value relation. It returns a new array with the number and or floats manipulated to strings.
// Filled array
$args = array(56, 68, 58.25, 235, 'string', 85.58, array(52.35, 66));
function NumberToString($arr)
{
$arr = (is_array($arr)) ? $arr : (array)$arr;
// New array
$NewArray = array();
// Loop through the array
foreach ($arr as $key => $value)
$NewArray[$key] = (is_array($value)) ? NumberToString($value) : (string)$value;
return $NewArray;
}
var_dump(NumberToString($args));
Output
array(7) {
[0]=> string(2) “56″
[1]=> string(2) “68″
[2]=> string(5) “58.25″
[3]=> string(3) “235″
[4]=> string(6) “string”
[5]=> string(5) “85.58″
[6]=> array(2) {
[0]=> string(5) “52.35″
[1]=> string(2) “66″
}
}
[0]=> string(2) “56″
[1]=> string(2) “68″
[2]=> string(5) “58.25″
[3]=> string(3) “235″
[4]=> string(6) “string”
[5]=> string(5) “85.58″
[6]=> array(2) {
[0]=> string(5) “52.35″
[1]=> string(2) “66″
}
}
Mail this!
- Comments (0)
- PingBacks (0)
- TrackBacks (0)

» Latest comments