yours_ali86 Posted September 3, 2007 Share Posted September 3, 2007 Please Help me in this regard, Your quick response will be appreciated, Create a PHP-based form to gather the names and ages of a list of people. Sort the list by the middle alphabet of the person's name. In the case that the name has an even number of characters, calculate the alphabet that lies between the two in consideration, and use that as the sorting factor. E.g. If the name is Mary, sort by the alphabet that lies between 'a' and 'r', leaning towards 'a'. Therefore, in this case the sorting alphabet would be 'i'. Output results to a table showing the name, age, and sorting alphabet. E.g. Name Age Sorting Alphabet Michael 22 h Mary 35 i Peter 30 t This does NOT require you to store any of the information in a database. Thanks A Lot edit removed unnecessary bold... Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 3, 2007 Share Posted September 3, 2007 Please don't post your topic in bold. Would something like this do it? <?php $people = array( array( 'name' => 'Peter', 'age' => 30, ), array( 'name' => 'Mary', 'age' => 35, ), array( 'name' => 'Michael', 'age' => 22, ), ); class PeopleSorter { private $people = array(); private $sorted = array(); public function __construct(array $people=array()) { $this->people = $people; $this->sort(); } private function sort() { $middle_chars = array(); foreach($this->people as $person) { $middle_chars[] = $person['name'][ceil((strlen($person['name'])-1)/2)]; } $this->sorted = $this->people; array_multisort($middle_chars, $this->sorted); } public function get_sorted() { return (array) $this->sorted; } } $people = new PeopleSorter($people); echo "<ol>\n"; foreach($people->get_sorted() as $person) { echo "\t<li>{$person['name']}, {$person['age']}</li>\n"; } echo "</ol>\n"; ?> Output will be: <ol> <li>Michael, 22</li> <li>Mary, 35</li> <li>Peter, 30</li> </ol> Edit: Note: If the name has an even number of characters (e.g. Daniel which has six characters), then it will take the right-most middle character. E.g. the middle character in "Daniel" would be 'i'. Edit 2: I didn't really get why you said "using OOP". What does the actual work is PeopleSorter::sort(). The reason why I made the class is because you specifically said "using OOP". Quote Link to comment Share on other sites More sharing options...
yours_ali86 Posted September 3, 2007 Author Share Posted September 3, 2007 Buddy Thanks for you Kind co-operation The reson behind this that "i said using OOP" is it was a part of a project where it is a must to use OOP. Dear Please explain this that how i will get the center character of the world "STRING","COMPUTER". if a consider your code you said "Edit: Note: If the name has an even number of characters (e.g. Daniel which has six characters), then it will take the right-most middle character. E.g. the middle character in "Daniel" would be 'i'. " But please read mine lines again,the lines are "E.g. If the name is Mary, sort by the alphabet that lies between 'a' and 'r', leaning towards 'a'. Therefore, in this case the sorting alphabet would be 'i'. " Where as you have specified in your code that right most word will be counted as Middle Char. Please do solve this problem. Thanks. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 3, 2007 Share Posted September 3, 2007 This: $person['name'][ceil((strlen($person['name'])-1)/2)]; is what gets the middle character, and to get it like you want it then it'll be: $person['name'][floor((strlen($person['name'])-1)/2)]; To understand how it works we'll split it up... You take the length by saying strlen($person['name']). Then you subtract 1 from it since we'll be working with the string as an array and the indexes in an array is starting from 0 and not 1. So we have strlen($person['name'])-1. Then we'll divide it by 2 to find the character in the middle. That means if the name was Daniel then we will have 2.5 now. There is no 2.5 character so we use floor() to round it to the next lowest integer which is 2 (or, well... the position of the middle character). So now we know that 2 is the middle character so we simply get the character by accessing the string as an array: $person['name'][floor((strlen($person['name'])-1)/2)]; or in this case: $person['name'][2]; You could write it like this as well: <?php $name = 'Daniel'; $string_length = strlen($name); $var = $string_length-1; $middle_character_pos = $var/2; $middle_character_pos = floor($middle_character_pos); $middle_character = $name[$middle_character_pos]; echo $middle_character; // output: n ?> But this is much shorter: <?php $name = 'Daniel'; echo $name[floor((strlen($name)-1)/2)]; ?> Using the string $name you could output Daniel in the following way: <?php $name = 'Daniel'; echo $name[0].$name[1].$name[2].$name[3].$name[4].$name[5]; // OR for($i=0; $i<=strlen($name)-1; $i++) { echo $name[$i]; } ?> because you can access strings like arrays. (you shouldn't do that however - it would be stupid) Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 5, 2007 Share Posted September 5, 2007 I thought we don't do homework here? 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.