![]() |
Given is an integer Array, the task is to convert the Integer array to a String array and print the output. Example: Input: These are the following approaches: Table of Content Using the array_map() and strval() functionIn 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.
Output Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 ) Using a loop and String castingIn 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.
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() functionIn 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().
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() FunctionThe 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:
Output Converted String Array: Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 ) |
Reffered: https://www.geeksforgeeks.org
PHP |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 21 |