Solved

Sorting array returns 1

Posted on 2008-10-09
4
893 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
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 125 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 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)
 

?>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
File submission to SQL Server 1 38
Php pie charts 3 26
PHP - AJAX and MySQL it works only if the value is a number 12 34
How to loop through records  using SELET COUNT 2 14
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now