Solved

# Sorting array returns 1

Posted on 2008-10-09
873 Views
Hello,

I'm using the array in the snippet beow.

You'll see that the third item has a lower index than the second. When I try to apply sort() to the array it just returns 1.

How can I sort the array so that they display in numerical index order?

Thanks
``````Array (

[1209575532] => Array (

[id] => 1 [title] => News Headline! [date] => 02-05-2008 [text] => Pellentesque id orci.

)

[1217524332] => Array (

[id] => 2 [title] => New News! [date] => 09-08-2008 [text] => Morbi commodo.

)

[1214845932] => Array (

[id] => 3 [title] => Go News! [date] => 06-07-2008 [text] => Quisque condimentum.

)

)
``````
0
Question by:Mr_Splash
• 2

LVL 48

Accepted Solution

hernst42 earned 125 total points
ID: 22678707
use ksort(\$array) to sort the array by it's keys
0

LVL 3

Author Comment

ID: 22678846
Hi Hernst,

using ksort also returns 1.

print_r(\$array); = returns the text in my snippet above.

print_r(ksort(\$array)); = returns 1

Any ideas?
0

LVL 48

Expert Comment

ID: 22678873
Yes thats correct, if you had read the documentation of ksort/sort and it's return value
0

LVL 3

Assisted Solution

DerkArts earned 125 total points
ID: 22678907
What hernst means is that ksort return a boolean, this tells you if the sorted succeed.

Try this:

``````<?

\$bool = ksort(\$array);

if(\$bool){

print_r(\$array);

}else{

echo 'could not sort';

}

// or just:

ksort(\$array);

print_r(\$array)

?>
``````
0

## Featured Post

### Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …