First, you need to explode your array
. That will create an indexed array, which starts the index from 0 up to 2. Next, you will need to implode it in some manner. Other answers are more explicit and use foreach
, so this one provides an alternative, where you convert your array into the desired format via array_map. The function
passed to array_map
is a so-called callback, a function
that is executed by array_map
for each element, in this case, which is aware of the key and the value of each element. Now, we implode our input and, to make sure that we have some keys, we pass the result of array_keys.
$input = explode(",", "Hello,Howdy,Hola");
$output = implode('<br>', array_map(
function ($v, $k) {
return ($k + 1).'. '.$v;
},
$input,
array_keys($input)
));