How to sort array by key and value without losing the original key value?

Asked

Viewed 98 times

0

I’ve tried every way.

The pure array is like this:

Array
(
 [71] => 100
 [70] => 100
 [69] => 23.53
 [68] => 74.07
 [67] => 23.29
 [66] => 61.01
 [59] => 100
 [3] => 35
 [1] => 56.18
)

When I apply a asort($array) gets like this:

Array
(
 [67] => 23.29
 [69] => 23.53
 [3] => 35
 [1] => 56.18
 [66] => 61.01
 [68] => 74.07
 [71] => 100
 [70] => 100
 [59] => 100
)

When I try a rsort($array), loses the original value of Key.

Array
(
 [0] => 100
 [1] => 100
 [2] => 100
 [3] => 74.07
 [4] => 61.01
 [5] => 56.18
 [6] => 35
 [7] => 23.53
 [8] => 23.29
)   

I need you to stay like this:

Array
(
 [59] => 100
 [70] => 100
 [71] => 100
 [68] => 74.07
 [66] => 61.01
 [1] => 56.18
 [3] => 35
 [69] => 23.53
 [67] => 23.29
)

If it was a SELECT in Mysql, it would be easy. Just apply an ORDER BY DESC KEY, ASC VALUE. But at Array, I can’t do it.

  • 1

    What is the expected result?

  • I edited the question and entered the result I need at the end.

  • 1

    Sorting is by value?

  • Tried with the uasort?

  • Tries the arsort

  • The arsort almost worked. All we had to do was let the KEY up. It looks like this: Array ( [71] => 100 [70] => 100 [59] => 100

Show 1 more comment

2 answers

2


Like asort worked, you can continue using and revert the array using array_reverse with the second parameter true, leaving the second parameter as true their keys are not changed.

<?php

$array = array
(
 71 => 100,
 70 => 100,
 69 => 23.53,
 68 => 74.07,
 67 => 23.29,
 66 => 61.01,
 59 => 100,
 3 => 35,
 1 => 56.18
);


echo "<pre>";
var_export($array);
asort($array);
var_export(array_reverse($array, true));

Exit

Original array

array (
  71 => 100,
  70 => 100,
  69 => 23.530000000000001,
  68 => 74.069999999999993,
  67 => 23.289999999999999,
  66 => 61.009999999999998,
  59 => 100,
  3 => 35,
  1 => 56.18,
)

Ordered array

array (
  59 => 100,
  70 => 100,
  71 => 100,
  68 => 74.069999999999993,
  66 => 61.009999999999998,
  1 => 56.18,
  3 => 35,
  69 => 23.530000000000001,
  67 => 23.289999999999999,
)

Execute

  • Wictor, note that I need the Decreasing values and the Rising keys.

  • I edited with the output the way you want :)

  • 1

    Wictor. It worked out! Dude... I can’t believe it! All day long. In Stack in English, no one could. Thanks anyway.

0

Although the answer accepts to work for this specific array, it may not work for other arrays. The array_reverse only worked because the original array already had keys in descending order.

To sort the array with decreasing values and increasing keys, in this order of importance, that is, by placing the keys of equal values in ascending order, you need to first sort the keys in ascending order and then sort the values in descending order.

Input array

$arr = array(
 71 => 100,
 70 => 100,
 69 => 23.53,
 68 => 74.07,
 67 => 23.29,
 66 => 61.01,
 59 => 100,
 3 => 35,
 1 => 56.18,
);

ksort - Sort keys from smallest to largest

ksort($arr);

arsort - Sort the values of the largest pro minor by holding the keys

arsort($arr);

Output Array

Array ( 
    [59] => 100 
    [70] => 100 
    [71] => 100 
    [68] => 74.07 
    [66] => 61.01 
    [1] => 56.18 
    [3] => 35 
    [69] => 23.53 
    [67] => 23.29 
)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.