flatronn Posted October 2, 2012 Report Share Posted October 2, 2012 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'); Quote Link to comment Share on other sites More sharing options...
falkencreative Posted October 2, 2012 Report Share Posted October 2, 2012 What portion of this are you struggling with specifically? What have you written so far? Quote Link to comment Share on other sites More sharing options...
flatronn Posted October 3, 2012 Author Report Share Posted October 3, 2012 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 Quote Link to comment Share on other sites More sharing options...
Andrea Posted October 3, 2012 Report Share Posted October 3, 2012 (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" Quote Link to comment Share on other sites More sharing options...
flatronn Posted October 3, 2012 Author Report Share Posted October 3, 2012 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 Quote Link to comment Share on other sites More sharing options...
Andrea Posted October 3, 2012 Report Share Posted October 3, 2012 Then why dont you bugger off and stop wasting my time Because I thought I could help you in a different way as well. But if you don't care about manners and punctuation, that's ok with me. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted October 3, 2012 Report Share Posted October 3, 2012 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; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.