-2

I am interested to sort php array index key in ascending order,

I have this array

 (
[462 1 5.300] => 1
[462 9 4.900] => 1
[462 9 4.300] => 1
[462 2 4.800] => 1
[462 6 4.700] => 1
[462 7 4.900] => 1
[462 7 4.700] => 1
[462 8 4.500] => 1
[462 3 4.500] => 1
[462 4 4.700] => 1
[462 3 4.700] => 1
[462 5 4.500] => 1
)

I expect output to be like this after sort

(
[462 1 5.300] => 1
[462 2 4.800] => 1
[462 3 4.500] => 1
[462 3 4.700] => 1
[462 4 4.700] => 1
[462 5 4.500] => 1
[462 6 4.700] => 1
[462 7 4.700] => 1
[462 7 4.900] => 1
[462 8 4.500] => 1
[462 9 4.300] => 1
[462 9 4.900] => 1
)

I want to sort first 3 index which is used as string is it possible ? I have 3 more column in array but I am showing only 3 columns here..

1
  • 1
    So what code did you write for that.? Post your code Commented Mar 20, 2014 at 15:01

3 Answers 3

3

A normal ksort() can do the trick, but if the sorting is to be performed in multiple orders (different orders for different parameter values), this wouldn't work.

You can achieve this with array_multisort(). First you create three (or more, depending on the number of parameters you have) arrays - each containing the parameter values. Then simply pass it to array_multisort() with the corresponding sort flag:

$param1 = $param2 = $param3 = array();

foreach ($data as $key => $value) {
    $parts = explode(' ', $key);
    $param1[] = $parts[0];
    $param2[] = $parts[1];
    $param3[] = $parts[2];
}

array_multisort($data, $param1, SORT_ASC, $param2, SORT_ASC, $param3, SORT_ASC);

If you want the sorting to be performed in descending order, then you can use SORT_DESC instead. See the documentation for a list of available sort flags.

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

+1 your demo is really helpful for newbie like me, Thanks a lot I am accepting and its easy to understand even
2

The function usort() would be the answer, but you need to sort by key, not by value. So you'd have to use uksort().

Tiny help for you (not tested):

<?php
function cmp($a, $b)
{
    $aParts = explode(' ',$a);
    $bParts = explode(' ',$b);
    if($aParts[0] !== $bParts[0]) {
      return ($aParts[0] >= $bParts[0]) ? -1 : 1;
    }
    if( (int) $aParts[1] !== (int) $bParts[1]) { // compare second parts as integer
      return ($aParts[1] >= $bParts[1]) ? -1 : 1;
    }
    if( (float) $aParts[2] !== (float) $bParts[2]) {
      return ($aParts[2] >= $bParts[2]) ? -1 : 1;        
    }
    return 0;
}

$a = array(
[462 1 5.300] => 1
[462 9 4.900] => 1
[462 9 4.300] => 1
[462 2 4.800] => 1
[462 6 4.700] => 1
[462 7 4.900] => 1
[462 7 4.700] => 1
[462 8 4.500] => 1
[462 3 4.500] => 1
[462 4 4.700] => 1
[462 3 4.700] => 1
[462 5 4.500] => 1
);

uksort($a, "cmp");

foreach ($a as $key => $value) {
    echo "$key: $value\n";
}

2 Comments

But I have many columns in array like date, time all are as string is it possible to use only few columns for uksort
that's what i have done. I only check the first three elements of the key. I also added casting so the second is compared as an integer and the third as a float.
1

In order to get the output you are showing, you can use the ksort() function -http://www.php.net/manual/fr/function.ksort.php.

<?php
$test = array(
"462 1 5.300" => 1,
"462 9 4.900" => 1,
"462 9 4.300" => 1,
"462 2 4.800" => 1,
"462 6 4.700" => 1,
"462 7 4.900" => 1,
"462 7 4.700" => 1,
"462 8 4.500" => 1,
"462 3 4.500" => 1,
"462 4 4.700" => 1,
"462 3 4.700" => 1,
"462 5 4.500" => 1
);

print_r($test);
ksort($test);
print_r($test);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.