How to convert an array of ints to an array of strings in PHP

1 Answer

0 votes
function convertToStringArray(array $numbers): array {
    // Map each integer to a string
    return array_map('strval', $numbers);
}

$numbers = [1, 2, 3, 4, 5];

// Convert the array of integers to a string array
$stringArray = convertToStringArray($numbers);

echo "String array:\n";
foreach ($stringArray as $str) {
    echo $str . "\n";
}


  
/*
run:
      
String array:
1
2
3
4
5

*/

 



answered Apr 1, 2025 by avibootz
...