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!
Advertisement
Answer
You need to use version_compare
function inside of sort function:
<?php $json = json_decode('{ "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" } }',true); function order($a, $b) { return version_compare($a['sirano'], $b['sirano']); } usort($json, 'order'); echo'<pre>'; print_r($json);
For descending change places $b
abd $a
variables:
return version_compare( $b['sirano'],$a['sirano']);