Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following php array, and would like to print it as a clean JSON file, but i'm getting extra quotes around the brackets and comma's. Although the current JSON i get is valid JSON it's not how I want it formatted. See PHP array below, my json_encode output and the preferred json output:

    Array
 (
[0] =>             {
[1] =>                 code : "EU27",
[2] =>                 debtA : 967576248244,
[3] =>                 debtB : 6971817122583,
[4] =>                 id : "eur",
[5] =>                 name : "EU",
[6] =>                 popY : 8584663069945,
[7] =>                 popZ : 7565913300548            }
[8] =>             ,
[9] =>             {
[10] =>                 code : "US",
[11] =>                 debtA : 9090198981283,
[12] =>                 debtB : 92189801202,
[13] =>                 id : "usa",
[14] =>                 name : "United States",
[15] =>                 popY : 4514703786570,
[16] =>                 popZ : 774697283542            }
    )

Current json_encode output:

   [
" {",
" code : \"EU27\",",
" debtA : 1739414366187,",
" debtB : 8719158563660,",
" id : \"eur\",",
" name : \"EU\",",
" popY : 460572909944,",
" popZ : 2396933253407 }",
" ,",
" {",
" code : \"US\",",
" debtA : 7810766144794,",
" debtB : 10536751929567,",
" id : \"usa\",",
" name : \"United States\",",
" popY : 8571487476842,",
" popZ : 1716024078740 }"
    ]

Preferred JSON output:

   [
 {
  code : "EU27",
  debtA : 1739414366187,
  debtB : 8719158563660,
  id : "eur",
  name : "EU",
  popY : 460572909944,
  popZ : 2396933253407 },
{
  code : "US",
  debtA : 7810766144794,
  debtB : 10536751929567,
  id : "usa",
  name : "United States",
  popY : 8571487476842,
  popZ : 1716024078740 }
   ]

I have been searching stackoverflow and found similar problem but can not get it to work. Probably because of my understanding of php and php arrays. Any help is appreciated.

share|improve this question
Why do you want the properties without quotes? Thats not valid json – sroes Mar 14 at 8:21
Did you create that array? You should use a 2-dimensional array for this purpose. – Antony Mar 14 at 8:27
Just implode your array values. – revoua Mar 14 at 8:28

4 Answers

The JSON string you get is 100% valid and corresponds exactly to the input array you've built. json_encode() will never attempt to make the guessing game you take for granted: if you have a string that contains { PHP will generate a string that contains {—it won't even consider the possibility of starting an object!

You obviously need to build your array this way:

$input = array(
    array(
        'code' => 'EU27',
        'debtA' => '1739414366187',
        'debtB' => '8719158563660',
        'id' => 'eur',
        'name' => 'EU',
        'popY' => '460572909944',
        'popZ' => '2396933253407',
    ),
    array(
        'code' => 'US',
        'debtA' => '7810766144794',
        'debtB' => '10536751929567',
        'id' => 'usa',
        'name' => 'United States',
        'popY' => '8571487476842',
        'popZ' => '1716024078740',
    ),
);

$output = json_encode($input);

Additionally, key names must be quoted—it isn't an option because they are strings:

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

share|improve this answer

First of you are making array of improper syntax:

[0] => {

Secondly the result you are looking for is an array of objects. This said; you will want to do something like the following:

$array = array();

$array[0] = new StdObject;
$array[0] -> code = "EU27";
// other values added to object

$array[1] = new StdObject;
$array[1] -> code = "US";
// other values added to second object

$json = json_encode( $array );

Alternatively you can also use an array instead of an object:

$array = array();

$array[0] = array(
"code" => "EU27",
// other values
);

$array[1] = array(
"code" => "US",
// other values
);

$json = json_encode( $array );
share|improve this answer

The proper json you want is

'             [
                  {
                   "code" : "EU27",
                   "debtA" : "1739414366187",
                   "debtB" : "8719158563660",
                   "id" : "eur",
                   "name" : "EU",
                   "popY" : "460572909944",
                   "popZ" : "2396933253407" },
                 {
                   "code" : "US",
                   "debtA" : "7810766144794",
                   "debtB" : "10536751929567",
                   "id" : "usa",
                   "name" : "United States",
                   "popY" : "8571487476842",
                   "popZ" : "1716024078740" }
                    ]

This json corresponds to the multidimenstional array which should be like: `

            Array
              (
                  [0] => Array
                      (
                          [code] => EU27
                          [debtA] => 1739414366187
                          [debtB] => 8719158563660
                          [id] => eur
                          [name] => EU
                          [popY] => 460572909944
                          [popZ] => 2396933253407
                      )

                  [1] => Array
                      (
                          [code] => US
                          [debtA] => 7810766144794
                          [debtB] => 10536751929567
                          [id] => usa
                          [name] => United States
                          [popY] => 8571487476842
                          [popZ] => 1716024078740
                      )

              )`          

For better understaning see here http://php.net/manual/en/function.json-encode.php '

share|improve this answer

You can achieve this easily.

First Use array in associated form here is how:

 $a = [ 
 [
 'code'=>'EU27',
 'debtA' => 967576248244, .... etc
 ], 
 [
 'code'=>'Something',
 'debtA' => 874376347, .... etc
 ]
 ];

then cast inner arrays to objects

 foreach($a as $k = $v)$a[$k] = (object)$v;

then json encode it... End of tragedy.
Here what I tried as a sample:

 $a = [ 
 [
 'code'=>'EU27',
 'debtA' => 967576248244//, .... etc
 ], 
 [
 'code'=>'Something',
 'debtA' => 874376347//, .... etc
 ]
 ];

 foreach($a as $k => $v)$a[$k]=(object)$a[$k];
 echo json_encode($a);

And that is what I got:

 [{"code":"EU27","debtA":967576248244},{"code":"Something","debtA":874376347}]

Hope this helps.

share|improve this answer
JSON will quote the string keys ex: code will be "code". But it will not make a difference in implementation and assignments. – Ihsan Mar 14 at 8:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.