In my project with PHP Inside the JSON is the data I keep. And each of these data has sequence numbers
1.1,
1.1.1,
1.1.2,
1.1.3,
…,
1.1.10.
When I sorted, I noticed that 1.1.10 came after 1.1.1, whereas 1.1.9 wasn’t there yet.
Because it sorts alphabetically.
And I don’t know how to deal with it!
Edit: Added sample JSON data.
{
"2": {
"sirano": "1",
"personal": "4",
"tarihler": {
"baslangic": "2022-11-10",
"bitis": "2022-11-17"
},
"ilerleme": "0"
},
"124": {
"sirano": "1.1.1",
"personal": "4",
"tarihler": {
"baslangic": "2022-11-10",
"bitis": "2022-11-17"
},
"ilerleme": "0"
},
"125": {
"sirano": "1.1.2",
"personal": "4",
"tarihler": {
"baslangic": "2022-11-10",
"bitis": "2022-11-17"
},
"ilerleme": "0"
},
"126": {
"sirano": "1.1.3",
"personal": "4",
"tarihler": {
"baslangic": "2022-11-10",
"bitis": "2022-11-17"
},
"ilerleme": "0"
},
"127": {
"sirano": "1.1.4",
"personal": "4",
"tarihler": {
"baslangic": "2022-11-10",
"bitis": "2022-11-17"
},
"ilerleme": "0"
},
"128": {
"sirano": "1.1.5",
"personal": "4",
"tarihler": {
"baslangic": "2022-11-10",
"bitis": "2022-11-17"
},
"ilerleme": "0"
},
"129": {
"sirano": "1.1.6",
"personal": "4",
"tarihler": {
"baslangic": "2022-11-10",
"bitis": "2022-11-17"
},
"ilerleme": "0"
},
"130": {
"sirano": "1.1.7",
"personal": "4",
"tarihler": {
"baslangic": "2022-11-10",
"bitis": "2022-11-17"
},
"ilerleme": "0"
},
"131": {
"sirano": "1.1.8",
"personal": "4",
"tarihler": {
"baslangic": "2022-11-10",
"bitis": "2022-11-17"
},
"ilerleme": "0"
},
"132": {
"sirano": "1.1.9",
"personal": "4",
"tarihler": {
"baslangic": "2022-11-10",
"bitis": "2022-11-17"
},
"ilerleme": "0"
},
"133": {
"sirano": "1.1.10",
"personal": "4",
"tarihler": {
"baslangic": "2022-11-10",
"bitis": "2022-11-17"
},
"ilerleme": "0"
},
"134": {
"sirano": "1.2",
"personal": "4",
"tarihler": {
"baslangic": "2022-11-10",
"bitis": "2022-11-17"
},
"ilerleme": "0"
},
"135": {
"sirano": "1.3.1",
"personal": "4",
"tarihler": {
"baslangic": "2022-11-10",
"bitis": "2022-11-17"
},
"ilerleme": "0"
},
"136": {
"sirano": "1.3.2",
"personal": "4",
"tarihler": {
"baslangic": "2022-11-10",
"bitis": "2022-11-17"
},
"ilerleme": "0"
},
"137": {
"sirano": "1.4.1",
"personal": "4",
"tarihler": {
"baslangic": "2022-11-10",
"bitis": "2022-11-17"
},
"ilerleme": "0"
}
}
$data = json_decode($proje->JSONData, true);
function order($a, $b)
{
return $a['sirano'] - $b['sirano'];
}
uasort($data, 'order');
I tried like this, but the result is clear!
Please help me, I’m going crazy!
2
Answers
You need to use
version_compare
function inside of sort function:For descending change places
$b
abd$a
variables:uasort keeps the original keys and strnatcmp compare 2 strings using a "natural order" algorithm.