1
$arr1 = [1, 2, 3, 8];
$arr2 = [1, 2, 4, 9, 10];
$arr3 = [1, 2, 5, 11, 12];
$arrs = [$arr1, $arr2, $arr3];
arsort($arrs);

I have sorted the $arrs to change it to $arr3, $arr2, $arr1, My problem is that it kept its Array Key as it is, I want to rewrite these keys by its new order, so instead of

[2]$arr3 [1]$arr2 [0]$arr1

it becomes

[0]$arr3 [1]$arr2 [2]$arr1

I thought about explode()ing then implode()ing the array again But it didn't work because it is a MDArray like the following $arrs = implode(explode($arrs)); after arsort().

What is the best and shortest way to re[write][make] the array keys?

11
  • 3
    Why not simply use rsort() then? It's the a part of arsort() that's maintaining the original keys Commented Nov 8, 2017 at 12:14
  • 1
    So if you needed alternative sorting than simply reversal, you shouldn't be using either rsort() or arsort() Commented Nov 8, 2017 at 12:17
  • 2
    usort($arrs, function($a, $b) { return count($b) - count($a); }); sorts by count of values and resets the keys..... uasort($arrs, function($a, $b) { return count($b) - count($a); }); sorts by count of values and retains the original keys.... where's the problem? Commented Nov 8, 2017 at 12:22
  • 2
    Use the correct sort function, and you don't have the problem at all, don't need to do any kludgy workround to reset keys after the sort Commented Nov 8, 2017 at 12:26
  • 2
    Use the correct sort in the first place.... one function, not two functions, that's only the overhead of one function call, not two function calls; the code is readable, short, concise; it does exactly what is necessary, no more, no less... KISS Commented Nov 8, 2017 at 12:28

3 Answers 3

3

Just simply use array_values;

$arr1 = [1, 2, 3, 8];
$arr2 = [1, 2, 4, 9, 10];
$arr3 = [1, 2, 5, 11, 12];
$arrs = [$arr1, $arr2, $arr3];
arsort($arrs);

$arrs  = array_values($arrs);

This will reset the keys based on the order.

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

Comments

1
Answer recommended by PHP Collective

You only need rsort if you don't need to keep key

$arr1 = [1, 2, 3, 8];
$arr2 = [1, 2, 4, 9, 10];
$arr3 = [1, 2, 5, 11, 12];
$arrs = [$arr1, $arr2, $arr3];
rsort($arrs);
print_r($arrs);

DEMO

3 Comments

That would work If i just want to reverse the values, But i'm trying to order the array by the highest count array, then change the keys values to be as same as the new order.
rsort() DOES sort subarrays by count first then by values if necessary.
0

I think you need to use rsort() instead of arsort() because, the latter will preserve the indices when sorting, while the first will reorder the values regardless of the keys.

1 Comment

Yes, But the problem is that i want the keys to be changed and not oversee it.

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.