explode function return array with serial number

576 views Asked by At

I have an array like this $arr = Array ("A","E","I","O","U");

My question is using implode function how can I make the output like this

1.A
2.E
3.I
4.O
5.U
3

There are 3 answers

1
Saumya Rastogi On BEST ANSWER

You need to iterate over each values like this:

$arr = array("A","E","I","O","U");
foreach ($arr as $key => $value) {
    echo $key + 1 . ".{$value} <br>";
}

This will give you the desired Output as:

1.A 
2.E 
3.I 
4.O 
5.U 

Hope this helps!

0
Maulik On
$i = 1;
foreach ($arr as $v) {
    echo $i . '.' . $v . '<br>';
    $i++;
}

no need to use implode function. Just use foreach loop to iterate whole array.

0
LF-DevJourney On

use array_walk to traverse the array like this:

array_walk($array, function($v, $k)
{
  echo $k + 1 . '.' . $v . "<br>";
});