Re: Sorting a large text string alphabetically

From: Date: Tue, 22 Jun 2004 03:27:16 +0000
Subject: Re: Sorting a large text string alphabetically
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-188791@lists.php.net to get a copy of this message
On Mon, 21 Jun 2004 23:19:58 -0400, Andre Dubuc <aajdubuc@webhart.net> wrote: > > 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(); > $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); > > ?> > > I assumed this would work, but I get error messages (for every value in the > txt array) stating that '$txt' is not an array -- even though it shows up as > Array(Joe Blow, Sam Hill, Henry Forget, etc, etc). I'm totally confused here. > As you may gather, arrays are not one of my 'strong' points :> > > Warning: sort() expects parameter 1 to be array, string given in > /var/www/html/list.php on line 235 > > Warning: reset() [function.reset]: Passed variable is not an array or object > in /var/www/html/list.php on line 236 > > Warning: Variable passed to each() is not an array or object in > /var/www/html/list.php on line 237 > > What am I doing wrong? foreach($names as $key => $names){ You are reusing the same variable, $names is being overwritten. -- Greg Donald http://destiney.com/

« previous php.general (#188791) next »