I am trying to split this string into numbered lists..
str options is "Hello,Howdy,Hola".
I need the output to be
This is my code
$newstr = 'My values are' . explode(",", $str["options"]) . '<br/>';
However, This causes to only print the word Array. Please help me on this :((
a) explode()
with comma to make string as an array.
b) Iterate over it and concatenate number and text.
c) Save all these concatenated values to one string.
d) echo
this string at end.
<?php
$str["options"] = "Hello,Howdy,Hola";
$data = explode(",", $str["options"]);
$newstr = 'My values are'. PHP_EOL;
foreach($data as $key => $value){
$number = $key+1;
$newstr .= "$number.". $value . PHP_EOL;
}
echo $newstr;
Note:- you can use <br>
instead of PHP_EOL
as well.