?
Solved

Sorting array returns 1

Posted on 2008-10-09
4
Medium Priority
?
1,006 Views
Last Modified: 2012-08-13
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.
  ) 
)

Open in new window

0
Comment
Question by:Mr_Splash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 48

Accepted Solution

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

Author Comment

by:Mr_Splash
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

by:hernst42
ID: 22678873
Yes thats correct, if you had read the documentation of ksort/sort and it's return value
Read http://www.php.net/ksort
0
 
LVL 3

Assisted Solution

by:DerkArts
DerkArts earned 500 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)
 
?>

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question