sort numeric string array in php

前端 未结 4 718
傲寒
傲寒 2020-12-15 16:15

I have a php array like :

myarr[1] = \"1\",
myarr[2] = \"1.233\",
myarr[3] = \"0\",
myarr[4] = \"2.5\"

the values are actually strings but

4条回答
  •  囚心锁ツ
    2020-12-15 17:00

    You can convert your strings to real numbers (floats) and sort them afterwards:

    foreach ($yourArray as $key => $value) {
        $yourArray[$key] = floatval($value);
    }
    
    sort($yourArray, SORT_NUMERIC);
    

提交回复
热议问题