2012-12-18 2 views
0

가능한 중복 : 난는 PHP에서 알파벳 순서로 JSON 배열을 정렬

{ 
    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 
     } 
    ] 
} 

:
Sorting an associative array in PHP

나는이처럼 보이는 Jsion 배열이 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; 
} 

어떻게 장치의 이름으로 배열을 정렬 할 수 있습니까?

답변

5
usort($devices,function($a,$b) {return strnatcasecmp($a['name'],$b['name']);}); 

문서 : usort(), strnatcasecmp()

관련 문제