1

i have create json from an array but how we can make an empty json

$jsonrows['paymentmethods']['CC']=array()
json_encode($jsonrows['paymentmethods']['CC']=array())

currently output is like this

"CC":[]

what i need is

"CC":{}

please help me with this

3
  • $test = json_encode($jsonrows['paymentmethods']['CC']=array()); Commented May 10, 2016 at 7:03
  • $jsonrows['paymentmethods']['CC']=json_encode(array()) Commented May 10, 2016 at 7:07
  • try json_encode($jsonrows['paymentmethods']['CC']=new stdClass); Commented May 10, 2016 at 7:11

3 Answers 3

3

Use a class instead of an array:

var_dump(json_encode(new StdClass()));
Sign up to request clarification or add additional context in comments.

Comments

2

Try This

  $cc['CC'] = new stdClass ;
  echo json_encode($jsonrows['paymentmethods']=$cc);

Output is

{"CC":{}}

Comments

1

For code readability, I use $emptyObject = (object)[];

In the context of your example:

$jsonrows['paymentmethods']['CC']=array(); echo json_encode($jsonrows['paymentmethods']);

yields the undesired: {"CC":[]}

$jsonrows['paymentmethods']['CC']=(object)array(); echo json_encode($jsonrows['paymentmethods']);

gives what you want: {"CC":{}}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.