php array counter for shuffled array

Hi,

I have an array.

$names = array ("John","Mark","Sam","Peter");

shuffle($names);

      foreach ($names as $name) {
                            echo " $name ";
                  }
//result:  Mark Peter Sam John

How do I setup a counter and display a count for each result, as it is displayed??
//result:  1.Mark 2.Peter 3.Sam 4.John

Thanks in advance.
johnsonallstarsAsked:
Who is Participating?
 
Dave BaldwinFixer of ProblemsCommented:
I would do this.
$names = array ("John","Mark","Sam","Peter");

shuffle($names);
$cntr = 0;
foreach ($names as $name) {
  $cntr++;
  echo "$cntr. $name ";
  }

Open in new window

0
 
johnsonallstarsAuthor Commented:
ok, that works, easy enough.  One other thing….

if I wanted to say…

foreach ($names as $name) {
  $cntr++;
    if ($cntr = "1") {
         echo "$name ";
   }
 }

//result: 1.Mark
0
 
johnsonallstarsAuthor Commented:
I was missing ==, instead =
0
 
Dave BaldwinFixer of ProblemsCommented:
Cool, glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.