如何在php中按键对数组进行降序排序?

时间:2020-12-15 15:55:55

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

我需要我的outpur作为数组键降序

2 个解决方案

#1


2  

You can use krsort() for this

您可以为此使用krsort()

krsort($myval);

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

关于更多的php数组排序,您可以参考http://www.techyline.com/php-sorting-array-with-unique-value/

#2


2  

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:

您可以使用krsort“按键对数组进行反向排序,保持键与数据的相关性。这主要用于关联数组。“是这样的:

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

krsort($myval);

print_r($myval);

#1


2  

You can use krsort() for this

您可以为此使用krsort()

krsort($myval);

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

关于更多的php数组排序,您可以参考http://www.techyline.com/php-sorting-array-with-unique-value/

#2


2  

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:

您可以使用krsort“按键对数组进行反向排序,保持键与数据的相关性。这主要用于关联数组。“是这样的:

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

krsort($myval);

print_r($myval);