1

I have an array and when I run print_r($array) I get:

Array
(
    [XS] => 110
    [S] => 171
    [M] => 172
    [L] => 171
    [XL] => 171
    [2X] => 172
    [3X] => 154
    [4X] => 139
    [5X] => 47
    [6X] => 32
    [OSFA] => 12
    [FITTED S-M] => 12
    [FITTED L-XL] => 12
    [ADJ] => 52
    [YXS] => 15
    [YSM] => 20
    [YMD] => 20
    [YLG] => 20
    [YXL] => 17
    [TLG] => 1
    [TXL] => 1
    [T2X] => 1
    [T3X] => 3
    [S-M FLEX] => 3
    [M-L FLEX] => 3
    [L-XL FLEX] => 3
    [CHILD STRETCH] => 1
    [S/M] => 1
 )

I want to sort it descending, but arsort returns 1. I can sort it with for loop but I want the best solution. Any suggestion?

3
  • 5
    Yes, arsort returns 1 because it's a mutating function. After arsort, your $array has been sorted. Document. Commented Feb 1, 2013 at 11:23
  • Thank you i thought it returns an array Commented Feb 1, 2013 at 11:25
  • Possible duplicate of php sort() on array produces wrong result Commented Feb 3, 2018 at 15:06

1 Answer 1

2

use arsort($icon,SORT_NUMERIC); you can sort the data as numeric put ur data on $icon

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

2 Comments

it removes the keys i don't want that
$array = array('a'=>1,'b'=>2); arsort($array,SORT_NUMERIC); print_r($array); sort return boolens dn store them just use sort and print on next line

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.