In PHP, you can iterate over each element in the set and convert it to a string using type casting or the strval()
function.
In this example,
$intSet
.$stringSet
.$intSet
using a foreach
loop.$stringSet
.<?php
$intSet = new DsSet([1, 2, 3, 4, 5]);
$stringSet = new DsSet();
foreach ($intSet as $num) {
$stringSet->add((string)$num);
}
var_dump($stringSet);
?>
object(Ds\Set)#2 (5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" }
In this example,
$intSet
.$stringSet
.$intSet
using a foreach
loop.strval()
function and add it to the $stringSet
.<?php
$intSet = new DsSet([1, 2, 3, 4, 5]);
$stringSet = new DsSet();
foreach ($intSet as $num) {
$stringSet->add(strval($num));
}
var_dump($stringSet);
?>
object(Ds\Set)#2 (5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" }
In this tutorial, we learned How to Convert a Set of Integers to a Set of Strings in PHP language with well detailed examples.