Convert integers or floats to strings.

Views: 5 Last modified: August 03rd, 2011 Comments: 0

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″
}
}
VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

    Mail this!

    To: From:Sum {1+7} =  
    Anything to add ?

        You must be logged in to post a comment.