Seegan See
Seegan See

Reputation: 101

How to sort a array descending order by key in php?

I have an array like this

 $myval = array('name1'=>'google', 'name5'=>'yahoo', 'name3'=>'facebook','name2'=>'twitter','name4'=>'linkedin');

I want the output like this

name5 = yahoo
name4 = linkedin
name3 = facebook
name2 = twitter
name1 = google

I need my outpur as array key descending

Upvotes: 2

Views: 84

Answers (2)

Suji Kumar
Suji Kumar

Reputation: 46

You can use krsort() for this

krsort($myval);

For more php array sort you can refer http://www.techyline.com/php-sorting-array-with-unique-value/

Upvotes: 2

Fluffeh
Fluffeh

Reputation: 33512

You can use krsort "Sorts an array by key in reverse order, maintaining key to data correlations. This is useful mainly for associative arrays. " like this:

$myval = array(
    'name1'=>'google', 
    'name5'=>'yahoo', 
    'name3'=>'facebook',
    'name2'=>'twitter',
    'name4'=>'linkedin');

krsort($myval);

print_r($myval);

Upvotes: 2

Related Questions