Jump to content

Recommended Posts

Posted

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');

Posted

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

Posted

(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"

Posted

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

Posted

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.

Posted

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;

}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...