Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to choose the sorting of ksort?

Tags:

php

ksort

I have a use for ksort() and I need to choose the sorting order(ASC/DESC).

Is there a way of doing it without array_multisort()?

like image 306
Neta Meta Avatar asked Nov 27 '13 00:11

Neta Meta


People also ask

How do you sort array keys?

The ksort() function sorts an associative array in ascending order, according to the key. Tip: Use the krsort() function to sort an associative array in descending order, according to the key. Tip: Use the asort() function to sort an associative array in ascending order, according to the value.

How do I sort a 2d array in PHP?

The array_multisort() function returns a sorted array. You can assign one or more arrays. The function sorts the first array, and the other arrays follow, then, if two or more values are the same, it sorts the next array, and so on.

What function is used to sort the values in array and keep the keys intact?

Sorting associative arrays can be done in one of several ways: Sort by keys, leave key-value association intact: Use ksort(). Sort by keys in reverse order, leave key-value association intact: Use krsort(). Sort by values, leave key-value association intact: Use asort().

How do you sort an array in descending order in PHP?

rsort() - sort arrays in descending order. asort() - sort associative arrays in ascending order, according to the value. ksort() - sort associative arrays in ascending order, according to the key. arsort() - sort associative arrays in descending order, according to the value.


2 Answers

krsort() for descending order
ksort() for ascending order

like image 82
Matthew Avatar answered Oct 07 '22 09:10

Matthew


Also :

krsort() function to sort an associative array in descending order, according to the key.
&
ksort for ascending order
plus
arsort() function to sort an associative array in ascending order, according to the value.

like image 6
Aryan Aly Avatar answered Oct 07 '22 10:10

Aryan Aly