How to Convert Integer array to String array using PHP?
Given is an integer Array, the task is to convert the Integer array to a String array and print the output.
Example:
Input:
$integerArray = [10, 20, 30, 40, 50];
Output:
["10", "20", "30", "40", "50"]
These are the following approaches:
Table of Content
Using the array_map() and strval() Function
In this approach, we are using array_map() to apply the strval function to each element in $integerArray, converting all integers to strings, and stores the result in $stringArray. The print_r function then outputs the contents of $stringArray, showing the converted string values.
Example: The example shows How to Convert Integer array to String array using PHP using the array_map() and strval.
<?php
$integerArray = [10, 20, 30, 40, 50];
$stringArray = array_map('strval', $integerArray);
print_r($stringArray);
?>
Output
Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 )
Using a Loop and String Casting
In this approach, we are using the string type casting operator which converts the integer value to its string representation. It is a simple and straightforward way to perform type conversion in PHP.
Example: The example shows How to Convert Integer array to String array using PHP using a loop and (string) casting.
<?php
$numbers = [10, 20, 30, 40, 50];
$stringNumbers = [];
for ($i = 0; $i < count($numbers); $i++) {
$stringNumbers[] = (string) $numbers[$i];
}
echo "Original Integer Array: ";
print_r($numbers);
echo "\n";
echo "Converted String Array: ";
print_r($stringNumbers);
?>
Output
Original Integer Array: Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 ) Converted String Array: Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4]...
Using a Loop and strval() Function
In this approach, we are using the strval() function which is used to convert the integer values to strings because PHP can automatically handle the type conversions when its necessary.
Example: The example shows How to Convert Integer array to String array using PHP using a loop and strval().
<?php
$intArray = [10, 20, 30, 40, 50];
$stringArray = [];
for ($i = 0; $i < count($intArray); $i++) {
$stringArray[] = strval($intArray[$i]);
}
echo "Original Integer Array: ";
print_r($intArray);
echo "Converted String Array: ";
print_r($stringArray);
?>
Output
Original Integer Array: Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 ) Converted String Array: Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] ...
Using array_walk() Function
The array_walk() function allows you to apply a callback function to each element of an array. By using this function, you can convert each integer to a string directly within the original array.
Example:
<?php
$integerArray = [10, 20, 30, 40, 50];
array_walk($integerArray, function(&$value) {
$value = strval($value);
});
echo "Converted String Array: ";
print_r($integerArray);
?>
Output
Converted String Array: Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 )
Using implode() and explode()
In this approach, we first convert the entire integer array to a single string using implode(), then split it back into an array of strings using explode(). This method might seem unconventional, but it’s a creative way to achieve the conversion.
Example: This example demonstrates how to convert an integer array to a string array using implode() and explode()
<?php
$integerArray = [10, 20, 30, 40, 50];
// Convert the integer array to a single string
$combinedString = implode(',', $integerArray);
// Split the string back into an array of strings
$stringArray = explode(',', $combinedString);
echo "Original Integer Array: ";
print_r($integerArray);
echo "Converted String Array: ";
print_r($stringArray);
?>
Output
Original Integer Array: Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 ) Converted String Array: Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 )