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 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 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 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
How to Loop Through an Array using a foreach Loop in PHP? Given an array (indexed or associative), the task is to loop through the array using foreach loop. The foreach loop iterates through each array element and performs the operations. PHP foreach LoopThe foreach loop iterates over each key/value pair in an array. This loop is mainly useful for iteratin
2 min read
PHP Change strings in an array to uppercase Changing strings in an array to uppercase means converting all the string elements within the array to their uppercase equivalents. This transformation modifies the array so that every string, regardless of its original case, becomes fully capitalized.Examples:Input : arr[] = ("geeks", "For", "GEEks
3 min read
How to Convert a Given Number to Words in PHP? Given an integer N, your task is to convert the given number into words using PHP.Examples:Input: N = 958237764Output: Nine Hundred Fifty Eight Million Two Hundred Thirty Seven Thousand Seven Hundred Sixty FourInput: N = 5000Output: Five ThousandBelow are the approaches to convert a given number to
5 min read
How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar
6 min read