18

I need to convert a PHP array to JSON but I don't get what I expect. I want it to be an object that I can navigate easily with a numeric index. Here's an example code:

$json = array();
$ip = "192.168.0.1";
$port = "2016";
array_push($json, ["ip" => $ip, "port" => $port]);
$json = json_encode($json, JSON_PRETTY_PRINT);
// ----- json_decode($json)["ip"] should be "192.168.0.1" ----
echo $json;

This is what I get

[  
   [  
      "ip" => "192.168.0.1",
      "port" => "2016"
   ]
]

But I want to get an object instead of array:

{  
   "0": {  
      "ip": "192.168.0.1",
      "port": "2016"
   }
}
2
  • 2
    Why do you want the outer array to become a JSON object? If it's only got numeric keys, an array delivers the same information in slightly less space and is easier to work with on the JS side of things (you can use Array::forEach and Array::map for example). Commented Jan 30, 2016 at 12:14
  • @00Davo Thanks, because I thought it was easier to handle it when it's an object hhh, and I use it mostly on server-side (PHP) only :) P.S. Upvoted Commented Jan 30, 2016 at 13:09

6 Answers 6

41

You want to json_encode($json, JSON_FORCE_OBJECT).

The JSON_FORCE_OBJECT flag, as the name implies, forces the json output to be an object, even when it otherwise would normally be represented as an array.

You can also eliminate the use of array_push for some slightly cleaner code:

$json[] = ['ip' => $ip, 'port' => $port];
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but this is what I get: { "0": { "0": "ip => 192.168.0.1", "1": "port => 2016" } }
Your strings are not quite on the right place in your array_push. Try array_push($json, ["ip" => $ip, "port" => $port]);
10

just use only

$response=array();
$response["0"]=array("ip"     => "192.168.0.1",
                     "port"   => "2016");
$json=json_encode($response,JSON_FORCE_OBJECT);

Comments

6

Just in case if you want to access your objectivitized json whole data OR a specific key value:

PHP SIDE

 $json = json_encode($yourdata, JSON_FORCE_OBJECT);

JS SIDE

 var json = <?=$json?>;
 console.log(json);            // {ip:"192.168.0.1", port:"2016"}
 console.log(json['ip']);      // 192.168.0.1
 console.log(json['port']);    // 2016

Comments

5

To get array with objects you can create stdClass() instead of array for inner items like below;

<?PHP

    $json = array();
    $itemObject = new stdClass();
    $itemObject->ip = "192.168.0.1";
    $itemObject->port = 2016;

    array_push($json, $itemObject);
    $json = json_encode($json, JSON_PRETTY_PRINT);
    echo $json;

?>

A working example http://ideone.com/1QUOm6

1 Comment

imo this is the only proper answer. Especially if you want an object inside of an array.
5

In order to get an object and not just a json string try:

$json = json_decode(json_encode($yourArray));

If you want to jsonise the nested arrays as well do:

$json =json_decode(json_encode($yourArray, JSON_FORCE_OBJECT));

Comments

-1
$ip   = "192.168.0.1";
$port = "2016";
$json = array("response" => array("ip" => $ip, "port" => $port)); 
//IF U NEED AS JSON OBJECT
$json = [array("ip" => $ip, "port" => $port)]; //IF U NEED AS JSON ARRAY
$json = json_encode($json);
echo $json;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.