title: PHP: Sort JSON data by keys
body: <p>I want to sort a single line of JSON data by the keys alphabetically using PHP. So in the end:</p>
  
  <pre><code>{"one":"morning","two":"afternoon","three":"evening","four":"night"}
  </code></pre>
  
  <p>becomes:</p>
  
  <pre><code>{"four":"night","one":"morning","three":"evening","two":"afternoon"}
  </code></pre>
  
  <p>I've tried using <code>ksort</code> to no avail:</p>
  
  <pre><code>$icons = json_decode(file_get_contents("icons.json"));
  ksort($icons);
  foreach($icons as $icon =&gt; $code){...}
  </code></pre>