Re: Sorting a large text string alphabetically
| From: | Curt Zirzow | Date: | Tue, 22 Jun 2004 04:08:27 +0000 |
| Subject: | Re: Sorting a large text string alphabetically | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-188794@lists.php.net to get a copy of this message | ||
* Thus wrote Andre Dubuc:
> I'm trying to sort alphabetically a large text string filled with first and
> last names separated by ", " That would seem to be a simple task, but
> sort($txt) does not seem to work even when $txt is an array.
>
> The code :
>
> <?php
>
> $OK= "Joe Blow, Sam Hill, Henry Forget, etc, etc";
>
> $_SESSION['txt'] = "$OK";
> session_write_close();
Are you really wanting to do this? this is a lot of overhead.
> $txt = $_SESSION['txt'];
>
>
> $names = explode(", ", $txt);
> foreach($names as $key => $names){
>
> sort($names); //line 235
> reset($names); // line 236
> while (list($key, $val) = each($names)) { //line 237
> }
> }
>
> $sorted = implode(", ", $names);
> return($sorted);
Now for the real issue... I'm not sure what all the foreach and
while loops are for but to simply sort your list you need two lines:
$sorted = explode(", ", $txt); // actually is $tosort
sort($sorted); // but its sorted now.
Curt
--
First, let me assure you that this is not one of those shady pyramid schemes
you've been hearing about. No, sir. Our model is the trapezoid!