2

I am trying to get json_encode output as: {"match_all":{}}. The sample program which I have written is:

<?php
 $qarray=Array("match_all"=>Array());
 print_r(json_encode($qarray));
?>

But above is giving me below o/p: {"match_all":[]}

mickmackusa
49.3k13 gold badges98 silver badges165 bronze badges
asked Dec 14, 2017 at 14:39
0

6 Answers 6

3

What you want is an object not an array. This will generate the output you want.

$qarray = array("match_all" => new stdClass());
print_r(json_encode($qarray));

https://www.w3schools.com/js/js_json_objects.asp

answered Dec 14, 2017 at 14:48
Sign up to request clarification or add additional context in comments.

Comments

3

The result you get is expected because [] denotes a JSON array and not a JSON object. If you need an object you need to explicitly provide one e.g.

<?php
$qarray = Array("match_all"=>(object)Array());
print_r(json_encode($qarray));

However this has little practical importance because Json encode will correctly create objects when you give it associative arrays e.g.

<?php
$qarray = [ "match_all"=> [ "assoc" => true ] ];
print_r(json_encode($qarray));

Prints:

{"match_all":{"assoc":true}}

For the purposes of JSON encoding associative arrays are any arrays that don't have sequential numbers as indices.

Also, short array syntax is a bit easier on the eyes in my opinion but is not different to using Array()

answered Dec 14, 2017 at 14:42

1 Comment

apokryfos wins on speed and explanation! +1 (explanation is critical on low-hanging fruit)
2

PHP json_encode function has option flag as 2nd parameter. You can set it to JSON_FORCE_OBJECT, then will get result you want.

Try this code.

<?php
 $qarray=Array("match_all"=>Array());
 print_r(json_encode($qarray, JSON_FORCE_OBJECT));
?>
answered Dec 14, 2017 at 14:47

Comments

0

You should specify this is an object, and not an array :

$qarray=Array("match_all"=>(object) Array());
print_r(json_encode($qarray));
answered Dec 14, 2017 at 14:43

Comments

0

As an addon to the other provided answers, if you have multiple values it would be pretty annoying to cast them all as objects each time, so you could pretty easily just add a loop after the declaration to set all of the values to objects.

$qarray=Array("match_all"=>Array());
foreach($qarray as $key => $val) {
 if(!is_array($val)) continue;
 $qarray[$key] = (object)$val;
}
print_r(json_encode($qarray));

This makes the code more dynamic as no matter how many keys, their values will all be set as an object before the encoding as long as the value is an array.

answered Dec 14, 2017 at 14:47

Comments

-1

In PHP an array can have strings as the key (aka an associative array) and when JSON encoded this is converted into a JSON object ({}). If however the array only has numeric keys, then the array is encoded as a JSON array - a list ([]).

To create an empty JSON object, you need to create an empty object in PHP instead of an empty array.

$qarray = ['match_all' => (object)[]];
print_r(json_encode($qarray));
answered Dec 14, 2017 at 14:50

2 Comments

Probably because your answer is very similar to ones already posted.
Thanks, that was a typo :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.