在PHP中按字母顺序对json数组进行排序

时间:2012-12-18 22:28:38

标签: php json sorting

  

可能重复:
  Sorting an associative array in PHP

我有一个看起来像这样的Jsion数组:

{
    devices: [
        {
            name: " Server 00 ",
            ip: " 172.20.10.10 ",
            status: 0
        },
        {
            name: " Server  10 ",
            ip: " 172.20.10.12 ",
            status: 0
        },
        {
            name: " Server  01 ",
            ip: " 172.20.10.13 ",
            status: 1
        },
        {
            name: " Server 11 ",
            ip: " 172.20.10.15 ",
            status: 0
        }
    ]
}

我正在使用PHP将其转换为html表格,但我希望它们按字母顺序排列。这是我的PHP代码:

    private static function toHtml($output, $rmkeyworkxen = false) {
    $return = '';

    $devices = json_decode($output, true)['devices'];

    foreach($devices as $device) {
        if(startsWith(trim($device['name']), "Xen")&&$rmkeyworkxen == true) {
            $return .= '';
        }
        else {
            if($device['status'] == 0) {
                $state = "Online";
                $return .= "<tr class=\"success\"><td>";
            }
            else {
                $state = "Offline";
                $return .= "<tr class=\"error\"><td>";
            }

            $return .= $device['name'];
            $return .= '</td><td>';
            $return .= $device['ip'];
            $return .= '</td><td>';
            $return .= $state;
            $return .= '</td></tr>';
        }
    }
    return $return;
}

我如何按设备名称对数组进行排序?

0 个答案:

没有答案