3

I am trying to convert a dynamic data. How to get this JSON from PHP:

/*JSON*/
{
   "122240cb-253c-4046-adcd-ae81266709a6": {
      "item": {
          "0": "3"
      }
   }
}

This is what I have done, but it's not working:

/*PHP*/

$json = array("122240cb-253c-4046-adcd-ae81266709a6"=> array(
    "item" => array($form_item)
));

echo json_encode($json, JSON_FORCE_OBJECT + JSON_PRETTY_PRINT);

This is the result I am getting instead of the above.

{
"122240cb-253c-4046-adcd-ae81266709a6": {
    "0": {
        "item": 3
    }
}
9
  • 2
    your code is just working fine. it gives the exact output as you wanted. Commented Mar 13, 2018 at 5:21
  • I think you are confused the with the json lint data with normal one. Commented Mar 13, 2018 at 5:23
  • 1
    @DavidAddoteye Update the value of $form_item Commented Mar 13, 2018 at 8:10
  • 1
    What is in $form_item? Commented Mar 13, 2018 at 8:14
  • 1
    @Thamaraiselvam thank you. You are right. That is where the problem is coming problem. It is working now. Thanks again. Commented Mar 13, 2018 at 8:15

1 Answer 1

2

First, a bit of code to demonstrate what needs to be adjusted.

  1. Move the item key up one level (out of the lowest subarray)
  2. Quote-wrap your $form_item value to make it a string.

Code: (Demo)

$form_item = 3;
$original_json = array("122240cb-253c-4046-adcd-ae81266709a6"=> array(
    array("item" => $form_item)
));
echo json_encode($original_json, JSON_FORCE_OBJECT + JSON_PRETTY_PRINT);

echo "\n---\n";

$form_item = "3";
$desired_json = array("122240cb-253c-4046-adcd-ae81266709a6"=> array(
    "item" => array($form_item)
));
echo json_encode($desired_json, JSON_FORCE_OBJECT + JSON_PRETTY_PRINT);

Output:

{
    "122240cb-253c-4046-adcd-ae81266709a6": {
        "0": {
            "item": 3
        }
    }
}
---
{
    "122240cb-253c-4046-adcd-ae81266709a6": {
        "item": {
            "0": "3"
        }
    }
}

Now onto the more interesting part that tripped me up at first glance...

You are using a syntax with the options parameters that I've never seen before and is not mentioned on the json_encode() documentation page. You are listing multiple json constants and separating them with + instead of the pipes (|) like the manual demonstrates.

To explain why this is valid syntax, I must express what is happening "behind the scenes".

The constants are actually "bitmasks". Each constant is assigned a number.

JSON_HEX_TAG => 1
JSON_HEX_AMP => 2
JSON_HEX_APOS => 4
JSON_HEX_QUOT => 8
JSON_FORCE_OBJECT => 16
JSON_NUMERIC_CHECK => 32
JSON_UNESCAPED_SLASHES => 64
JSON_PRETTY_PRINT => 128
JSON_UNESCAPED_UNICODE => 256
JSON_PARTIAL_OUTPUT_ON_ERROR => 512
JSON_PRESERVE_ZERO_FRACTION => 1024

You see, these numbers are not arbitrarily assigned; each progressive number is deliberately twice the previous number. Why? Because if you dare to list multiple options, you can write a single number that represents the sum of any two or more constants and you will never accidentally fall prey to a value collision.

What does this mean? All of the following expressions produce the same output:

echo json_encode($json, JSON_FORCE_OBJECT | JSON_PRETTY_PRINT);
echo json_encode($json, JSON_FORCE_OBJECT + JSON_PRETTY_PRINT);
echo json_encode($json, 16 + 128);
echo json_encode($json, 144);

Want proof? (Demo)

Sign up to request clarification or add additional context in comments.

1 Comment

@David I have retracted my Off-topic: Typo close vote. You are using a syntax that I hadn't come across before. I am actually glad you did, because it gave me a reason to research the options constants that json uses.

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.