在JSON和PHP中排序点数字

sirbozc5  于 2022-11-19  发布在  PHP
关注(0)|答案(2)|浏览(99)

在我的PHP项目中,JSON里面是我保存的数据,每个数据的序列号是1.1,1.1.1,1.1.2,1.1.3,...,1.1.10。
当我排序时,我注意到1.1.10在1.1.1之后,而1.1.9还没有出现。
因为它是按字母顺序排序的。我不知道该怎么处理!
编辑:添加了示例JSON数据。

{
  "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');

我试过了,但结果很明显!请帮帮我,我快疯了!

a8jjtwal

a8jjtwal1#

您需要在sort函数中使用version_compare函数:

<?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);

对于降序变化位$b abd $a变量:

return version_compare( $b['sirano'],$a['sirano']);
qnakjoqk

qnakjoqk2#

uasort($array, fn($a,$b) => strnatcmp($a['sirano'],$b['sirano']));

x1E0F1x保持原始密钥,并且x1E1F1x使用“自然顺序”算法来比较2个字符串。

相关问题