How to Convert Integer array to String array using PHP?
Last Updated :
20 Aug, 2024
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:
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
<?php
$integerArray = [10, 20, 30, 40, 50];
$stringArray = array_map('strval', $integerArray);
print_r($stringArray);
?>
OutputArray
(
[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
<?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);
?>
OutputOriginal 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
<?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);
?>
OutputOriginal 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
<?php
$integerArray = [10, 20, 30, 40, 50];
array_walk($integerArray, function(&$value) {
$value = strval($value);
});
echo "Converted String Array: ";
print_r($integerArray);
?>
OutputConverted 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
<?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);
?>
OutputOriginal 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
)
Similar Reads
How to convert an Integer Into a String in PHP ?
The PHP strval() function is used to convert an Integer Into a String in PHP. There are many other methods to convert an integer into a string. In this article, we will learn many methods.Table of ContentUsing strval() function.Using Inline variable parsing.Using Explicit Casting.Using sprintf() Fun
3 min read
How to convert array of strings to array of numbers in JavaScript ?
Converting an array of strings to an array of numbers in JavaScript involves transforming each string element into a numerical value. This process ensures that string representations of numbers become usable for mathematical operations, improving data handling and computational accuracy in applicati
4 min read
How to Convert Number to Character Array in PHP ?
Given a number, the task is to convert numbers to character arrays in PHP. It is a common operation when you need to manipulate or access individual digits of a number. This can be particularly useful in situations where you need to perform operations on the digits of a number, such as digital root
3 min read
How to use array_merge() and array_combine() in PHP ?
In this article, we will discuss about how to use array_merge() and array_combine() functions in PHP. Both functions are array based functions used to combine two or more arrays using PHP. We will see each function with syntax and implementation array_merge() FunctionThis function merges the two or
3 min read
How to convert DateTime to String using PHP ?
Converting a `DateTime` to a string in PHP involves formatting the `DateTime` object into a human-readable format using the `format()` method. This process allows you to represent dates and times as strings in various formats, such as Y-m-d H:i:s. There are some following approachesTable of ContentB
4 min read
How to convert a String into Number in PHP ?
Strings in PHP can be converted to numbers (float/ int/ double) very easily. In most use cases, it won't be required since PHP does implicit type conversion. This article covers all the different approaches for converting a string into a number in PHP, along with their basic illustrations.There are
4 min read
How to convert uppercase string to lowercase using PHP ?
Converting an uppercase string to lowercase means changing all capital letters in the string to their corresponding small letters. This is typically done using programming functions or methods to ensure uniformity in text formatting and comparison.Below we have some common approaches Table of Conten
2 min read
How to convert String to Float in PHP ?
Converting a string to a float in PHP is a common requirement when handling numeric data stored in string format. This conversion allows the numeric string to be used in calculations or comparisons, ensuring accurate manipulation of data within the program.There are many methods to convert a string
3 min read
How to convert an array to CSV file in PHP ?
To convert an array into a CSV file we can use fputcsv() function. The fputcsv() function is used to format a line as CSV (comma separated values) file and writes it to an open file. The file which has to be read and the fields are sent as parameters to the fputcsv() function and it returns the leng
2 min read
PHP Merging two or more arrays using array_merge()
The array_merge() function in PHP combines two or more arrays into one. It merges the values from the arrays in the order they are passed. If arrays have duplicate string keys, the latter values overwrite earlier ones, while numerical keys are re-indexed sequentially.Syntaxarray array_merge($array1,
2 min read