Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP > json_encode , force the creation of an array even if it has only one element

Tags:

json

php

I take an xml from an external service via POST , and return the xml in json

this is an example of the xml

<xml>
     <item>
          <user>utente1</user>
          <psw>A722C63DB8EC8625AF6CF71CB8C2D939</psw>
          <code>A722C63DB8EC8625AF6CF71CB8C2D939</code>
     </item>
     <item>
          <user>utente2</user>
          <psw>A722C63DB8EC8625AF6CF71CB8C2D939</psw>
          <code>A722C63DB8EC8625AF6CF71CB8C2D939</code>
     </item>
</xml>

and with this procedure i transforms the xml in json

php

$xml = simplexml_load_string($getPostData);
$json = json_encode($xml);

json result

{
    "item": [
        {
            "user": "utente1",
            "psw": "A722C63DB8EC8625AF6CF71CB8C2D939",
            "code": "25BBDCD06C32D477F7FA1C3E4A91B032"
        },
        {
            "user": "utente2",
            "psw": "A722C63DB8EC8625AF6CF71CB8C2D939",
            "code": "25BBDCD06C32D477F7FA1C3E4A91B032"
        }
    ]
}

and this is correct. but there is a problem when the xml have only one item

 <xml>
      <item>
           <user>utente1</user>
           <psw>A722C63DB8EC8625AF6CF71CB8C2D939</psw>
           <code>A722C63DB8EC8625AF6CF71CB8C2D939</code>
      </item>
 </xml>

in this case the json is

{
    "item": {
        "user": "utente1",
        "psw": "A722C63DB8EC8625AF6CF71CB8C2D939",
        "code": "25BBDCD06C32D477F7FA1C3E4A91B032"
    }
}

item is an object in this case , and not an array with one element.

I wish it were so

{
    "item": [
        {
            "user": "utente1",
            "psw": "A722C63DB8EC8625AF6CF71CB8C2D939",
            "code": "25BBDCD06C32D477F7FA1C3E4A91B032"
        }
    ]
}

I read the documentation of json_encode and the various json constants , 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


UPDATE

My solution

//$getPostData = "<xml><item><user>utente1</user><psw>A722C63DB8EC8625AF6CF71CB8C2D939</psw><code>25BBDCD06C32D477F7FA1C3E4A91B032</code></item><item><user>utente1</user><psw>A722C63DB8EC8625AF6CF71CB8C2D939</psw><code>25BBDCD06C32D477F7FA1C3E4A91B032</code></item></xml>";   

$getPostData = "<xml><item><user>utente1</user><psw>A722C63DB8EC8625AF6CF71CB8C2D939</psw><code>25BBDCD06C32D477F7FA1C3E4A91B032</code></item></xml>";  

$xml = simplexml_load_string($getPostData);

$json = json_encode($xml);
$jsonCheck = json_decode($json);

$numItem =  sizeof($jsonCheck->item);

if($numItem == 1){
    $newJson = new stdClass();
    $newJson->item = array();
    $newJson->item[0]["user"] = $jsonCheck->item->user;
    $newJson->item[0]["psw"] = $jsonCheck->item->psw;
    $newJson->item[0]["code"] = $jsonCheck->item->code;
    $newJson = json_encode($newJson);
    echo $newJson;
}else{
    echo $json;
}
like image 968
WhiteLine Avatar asked Jan 15 '15 09:01

WhiteLine


2 Answers

If this is the structure of the entire XML and not just a fragment from something bigger, you can turn it into an array or a mix of arrays and objects using less code:

$xml = simplexml_load_string($getPostData);

$array = array('item' => array());
foreach ($xml->item as $item) {
    $array['item'][] = (object)(array)$item;
}

echo(json_encode($array));

This will produce the output you described in the question as expected, no matter how many <item> elements appear in the XML.

Remove the (object) from the code inside the foreach() to get the <item> elements converted to arrays instead of stdClass objects.

like image 126
axiac Avatar answered Nov 03 '22 05:11

axiac


I had the same issue, took me some time to figure out but I fixed it like this.

$somearray = array();

$_SESSION['activeChatBoxes'] = array_merge($somearray, $_SESSION['activeChatBoxes']);

If the session var activeChatBoxes only contains one item, it would be json_encoded as an Object. After merging into an empty array, the json_encode properly shows an array like

["string"] 

Instead of:

{ 1, "string" }
like image 42
Coen Damen Avatar answered Nov 03 '22 06:11

Coen Damen