Re: issues with ksort()

From: Date: Fri, 07 Feb 2003 17:51:50 +0000
Subject: Re: issues with ksort()
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-134762@lists.php.net to get a copy of this message
Ah.... Thanks! -Shawn ----- Original Message ----- From: "Paul Chvostek" <paul+php@it.ca> To: "Shawn McKenzie" <nospam@mckenzies.net> Cc: <php-general@lists.php.net> Sent: Friday, February 07, 2003 11:18 AM Subject: Re: issues with ksort() On Fri, Feb 07, 2003 at 11:09:10AM -0600, Shawn McKenzie wrote: > > What else does ksort() do to the array??? Does it matter that my keys look > like this: '[some-text]'??? > > This works great: > > foreach($myarray as $key => $val) { > echo "$key = $val"; > } > > This gives me a Warning: Invalid argument supplied for foreach(): > > $sortedarray = ksort($myarray); > foreach($sortedarray as $key => $val) { > echo "$key = $val"; > } > > Any ideas??? Inserting a print_r into your test script tells you the problem, as does a closer look at http://www.php.net/ksort . You're assuming ksort()'s return value is the sorted array. It is not. Try something more along the lines of: $testarray=array( "[three]" => 3, "[two]" => 2, "[one]" => 1, ); ksort($testarray); print_r($testarray); -- Paul Chvostek <paul@it.ca> Operations / Abuse / Whatever it.canada, hosting and development http://www.it.ca/

« previous php.general (#134762) next »