title: PHP > json_encode , force the creation of an array even if it has only one element
body: <p>I take an xml from an external service via POST , and return the xml in json</p>
  
  <p>this is an example of the xml</p>
  
  <pre><code>&lt;xml&gt;
       &lt;item&gt;
            &lt;user&gt;utente1&lt;/user&gt;
            &lt;psw&gt;A722C63DB8EC8625AF6CF71CB8C2D939&lt;/psw&gt;
            &lt;code&gt;A722C63DB8EC8625AF6CF71CB8C2D939&lt;/code&gt;
       &lt;/item&gt;
       &lt;item&gt;
            &lt;user&gt;utente2&lt;/user&gt;
            &lt;psw&gt;A722C63DB8EC8625AF6CF71CB8C2D939&lt;/psw&gt;
            &lt;code&gt;A722C63DB8EC8625AF6CF71CB8C2D939&lt;/code&gt;
       &lt;/item&gt;
  &lt;/xml&gt;
  </code></pre>
  
  <p>and with this procedure i transforms the xml in json</p>
  
  <p><strong>php</strong> </p>
  
  <pre><code>$xml = simplexml_load_string($getPostData);
  $json = json_encode($xml);
  </code></pre>
  
  <p><strong>json result</strong></p>
  
  <pre><code>{
      "item": [
          {
              "user": "utente1",
              "psw": "A722C63DB8EC8625AF6CF71CB8C2D939",
              "code": "25BBDCD06C32D477F7FA1C3E4A91B032"
          },
          {
              "user": "utente2",
              "psw": "A722C63DB8EC8625AF6CF71CB8C2D939",
              "code": "25BBDCD06C32D477F7FA1C3E4A91B032"
          }
      ]
  }
  </code></pre>
  
  <p><strong>and this is correct</strong>. but there is a problem when the xml have only one item</p>
  
  <pre><code> &lt;xml&gt;
        &lt;item&gt;
             &lt;user&gt;utente1&lt;/user&gt;
             &lt;psw&gt;A722C63DB8EC8625AF6CF71CB8C2D939&lt;/psw&gt;
             &lt;code&gt;A722C63DB8EC8625AF6CF71CB8C2D939&lt;/code&gt;
        &lt;/item&gt;
   &lt;/xml&gt;
  </code></pre>
  
  <p>in this case the json is</p>
  
  <pre><code>{
      "item": {
          "user": "utente1",
          "psw": "A722C63DB8EC8625AF6CF71CB8C2D939",
          "code": "25BBDCD06C32D477F7FA1C3E4A91B032"
      }
  }
  </code></pre>
  
  <p>item is an object in this case , and not an array with one element.</p>
  
  <p>I wish it were so</p>
  
  <pre><code>{
      "item": [
          {
              "user": "utente1",
              "psw": "A722C63DB8EC8625AF6CF71CB8C2D939",
              "code": "25BBDCD06C32D477F7FA1C3E4A91B032"
          }
      ]
  }
  </code></pre>
  
  <p>I read the documentation of <a href="http://php.net/manual/it/function.json-encode.php" rel="noreferrer">json_encode</a> and the <a href="http://php.net/manual/it/json.constants.php" rel="noreferrer">various json constants</a> , but i can't find an option to force this procedure. I proceed with a manual control or i can do it automatically? 
  thank you</p>
  
  <hr>
  
  <h2>UPDATE</h2>
  
  <p>My solution</p>
  
  <pre><code>//$getPostData = "&lt;xml&gt;&lt;item&gt;&lt;user&gt;utente1&lt;/user&gt;&lt;psw&gt;A722C63DB8EC8625AF6CF71CB8C2D939&lt;/psw&gt;&lt;code&gt;25BBDCD06C32D477F7FA1C3E4A91B032&lt;/code&gt;&lt;/item&gt;&lt;item&gt;&lt;user&gt;utente1&lt;/user&gt;&lt;psw&gt;A722C63DB8EC8625AF6CF71CB8C2D939&lt;/psw&gt;&lt;code&gt;25BBDCD06C32D477F7FA1C3E4A91B032&lt;/code&gt;&lt;/item&gt;&lt;/xml&gt;";   
  
  $getPostData = "&lt;xml&gt;&lt;item&gt;&lt;user&gt;utente1&lt;/user&gt;&lt;psw&gt;A722C63DB8EC8625AF6CF71CB8C2D939&lt;/psw&gt;&lt;code&gt;25BBDCD06C32D477F7FA1C3E4A91B032&lt;/code&gt;&lt;/item&gt;&lt;/xml&gt;";  
  
  $xml = simplexml_load_string($getPostData);
  
  $json = json_encode($xml);
  $jsonCheck = json_decode($json);
  
  $numItem =  sizeof($jsonCheck-&gt;item);
  
  if($numItem == 1){
      $newJson = new stdClass();
      $newJson-&gt;item = array();
      $newJson-&gt;item[0]["user"] = $jsonCheck-&gt;item-&gt;user;
      $newJson-&gt;item[0]["psw"] = $jsonCheck-&gt;item-&gt;psw;
      $newJson-&gt;item[0]["code"] = $jsonCheck-&gt;item-&gt;code;
      $newJson = json_encode($newJson);
      echo $newJson;
  }else{
      echo $json;
  }
  </code></pre>