douguadao3883 2014-11-25 02:47
浏览 45
已采纳

PHP按名称排序数组

The data returning from my database is encrypted. so running a order by at the end of the SQL is not working. so I have to sort on the server side.

I see a lot a question about this I have tried a suggestions but I can't seem to get to sort . I tried this routine and this one (look like the closest to what I was looking for).

2 things are happening:

1: It don't sort by the name field(key)

2: Our in house app complains that its not a JSON string any longer.

I need to sort this my the name, so I need a custom sort

I get my array set like this:

$jsonData = array();

...connected to the DB.... stuff here...
//loop through the return:

if($result->num_rows > 0)
     {
        while($row = $result->fetch_assoc())
         {
               $row["name"] = $this->decryptData($row["name"]);
              $jsonData["groups"][] = $row ;

         }   

}


    $result->close();   
    mysqli_close($mysqli);
    return $jsonData;

The return code looks like this:

{
  "groups": [
    {
      "id": "71",
      "name": "Bob",
       },
    {
      "id": "73",
      "name": "Howard",
    },
    {
      "id": "79",
      "name": "Sam",

    },....
  ....{
      "id": "65",
      "name": "Al",

    }
  ]
}

OK so I added this code at the bottom of my routine to sort (notice I tried to methods):

.... code

        /*usort($jsonData, function($a, $b) {
           return $a['name'] - $b['name'];
        });*/

         usort($jsonData, function($a, $b) {
         return strcasecmp($a['name'], $b['name']);
         });

    /* and I tried this get the same output but with a php error say what I have is not a array
        array_multisort($jsonData['name']);
          -- and this--
        array_multisort($jsonData['groups']['name']);

   */



$result->close();   
        mysqli_close($mysqli);
        return $jsonData;

and I get this result which is little different than what I need and its not sorted by name:

[
   [
        {
          "id": "71",
          "name": "Bob",
           },
        {
          "id": "73",
          "name": "Howard",
        },
        {
          "id": "79",
          "name": "Sam",

        },....
      ....{
          "id": "65",
          "name": "Al",

        }
      ]
]

I'm lost at the moment.

  • 写回答

2条回答 默认 最新

  • duanlei5339 2014-11-25 02:50
    关注

    You can't apply arithmetics to string values for comparisons; you need a string function instead, such as strcmp() or strcasecmp() which return a value of -1, 0, or 1:

    usort($jsonData, function($a, $b) {
        return strcasecmp($a['name'], $b['name']);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?