2

Hi I have below array,

$arr = Array ( [0] => 248 [1] => 247 )

Using asort() I get below result,

$arr = Array ( [1] => 247 [0] => 248 )

But I need the below format,

$arr = Array ( [0] => 247 [1] => 248 )

Need to sort only the value not key. Is there any default function in php for my expected result?

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
Thiyagu
  • 746
  • 3
  • 11
  • 29
  • 2
    Possible duplicate of [PHP array multiple sort - by value then by key?](https://stackoverflow.com/questions/2282013/php-array-multiple-sort-by-value-then-by-key) – Billal Begueradj Jul 24 '17 at 13:06

2 Answers2

4

You can use array_values to reset the keys after sorting.

<?php

$arr = [248, 247];

asort($arr);

print_r(array_values($arr));

will give:

Array
(
    [0] => 247
    [1] => 248
)

BTW, I ain't sure why you use asort, but when you use sort function to sort, it resets the keys.

From the manual:

asort - Sort an array and maintain index association

sort - Sort an array (Your Exact Need)

Community
  • 1
  • 1
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
4

Use just sort() function.

You can see more about sorting functions in PHP here