Jump to content

Sorting a Numeric Array


Go to solution Solved by xProteuSx,

Recommended Posts

I have an array that looks like this:

 

Array ([0] => 1  [3] =>  [4] => 285 [6] => 190)

 

and I would like to sort it from highest value to lowest.

 

As is, when I print_r() this array, I get:

 

1

104

285

190

 

I would like to get:

 

1

104

190

285

 

I have tried the following:  sort(), asort(), ksort(), and krsort().

 

What else can I try?

 

My code looks like this:

 

$countries_array = array_unique($countries_array);
print_r($countries_array);
 
which returns:
 

1

104

285

190

 
 
However, when I do this:
 
$countries_array = array_unique($countries_array);
$countries_array = sort($countries_array);
print_r($countries_array);
 
It simply returns:  1
 
Thanks in advance.
Link to comment
https://forums.phpfreaks.com/topic/287866-sorting-a-numeric-array/
Share on other sites

  • Solution

And as is customary, I figure this all out about 2 seconds after I post for help.

 

The trick is that you don't need this:

 

$countries_array = sort($countries_array);
 
You simply do this:
 
sort($countries_array);
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.