1

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.

4
  • Why do you want the properties without quotes? Thats not valid json Commented Mar 14, 2013 at 8:21
  • Did you create that array? You should use a 2-dimensional array for this purpose. Commented Mar 14, 2013 at 8:27
  • Just implode your array values. Commented Mar 14, 2013 at 8:28
  • If the reason why the extra quotes bother you is because you need to transmit the data via HTTP, then I found that my preferred HTTP Library, Guzzle, doesn't need you to JSON-encode the data. Just give it an array, and it will convert it to proper JSON on the wire before making the request. -- got the tip via #php on Freenode irc. Commented Feb 14, 2017 at 20:20

4 Answers 4

1

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 );
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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 '

Comments

0

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.

1 Comment

JSON will quote the string keys ex: code will be "code". But it will not make a difference in implementation and assignments.

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.