First, a bit of code to demonstrate what needs to be adjusted.
- Move the
item key up one level (out of the lowest subarray)
- 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)
$form_item