Jump to content

Write a function to return a list of people


flatronn

Recommended Posts

Hi,

I have been given a excercise to do but am strugling with it the question is below if some one could enlighten me on how it could be done.

 

 

 

Write a function to return a list of people

This function should accept an array of people's names as an argument and return a string list of these names.

If there is a single person, the name should be returned as is.

If there are two people, the two names should be separated by the word ‘and’.

 

If there are more than two people, the last two names should be separated with the word ‘and’, and all previous names separated with a comma.

 

Test the function with the following arrays:

$test1 = array('Person One');

$test2 = array('Person One', 'Person Two');

$test3 = array('Person 1', 'Person 2', 'Person 3');

$test4 = array('Person 1', 'Person 2', 'Person 3', 'Person 4'

'Person 5', 'Person 6');

Link to comment
Share on other sites

What portion of this are you struggling with specifically? What have you written so far?

Hi ,

I have got here so far as below

<?php

$people = array("Peter", "Joe", "Glenn", "Cleveland");

 

reset($people);

 

while (list($key, $val) = each($people))

{

echo "$key => $val<br />";

}

?>

My problem how to get a comma between each person and then a and at the last name

So the string would look like: (peter,Joe,Glenn and Cleveland.)

CODING FOR THAT SEEMS VERY DIFFICULT.

 

Cheers

Link to comment
Share on other sites

(peter,Joe,Glenn and Cleveland.)

 

While I know nothing of PHP, I do know a little about grammar and punctuation, and it should be:

 

Peter, Joe, Glenn, and Cleveland - unless Glenn and Cleveland are a couple, in which case it should read "Peter, Joe, and Glenn and Cleveland"

Link to comment
Share on other sites

While I know nothing of PHP, I do know a little about grammar and punctuation, and it should be:

 

Peter, Joe, Glenn, and Cleveland - unless Glenn and Cleveland are a couple, in which case it should read "Peter, Joe, and Glenn and Cleveland"

Then why dont you bugger off and stop wasting my time

Link to comment
Share on other sites

Then why dont you bugger off and stop wasting my time

Andrea was simply trying to help. No need to be rude.

 

Personally, I'd go about this in an easier way -- create a temporary variable to store the result of the function, and simply use a for loop to loop though each item in the array. At the beginning of each loop, add a comma and a space ", " to the temporary variable unless it is the first item in the array, or the last item in the array. You can use count() on the array to check how many items are in the array, and compare that with your for() variable to see if it is the last item. If it is the last item, then add "and ", and then finally add the name from the array. After the for loop completes, return that variable.

 

Keep in mind that you want to create a function, which usually looks like this:

 

function your_function_name($argument)

{

...code here...

return $variable;

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...