-2

I'm having some issues with json_encode, i need that the output were something like this:

{
  "Advisories": [
    {
      "Advisory": {
        "Severe": "false",
        "description": "Lorem ipsum dolor sit amet",
        "title": "Advise:",
      }
    },
    {
      "Advisory": {
        "Severe": "true",
        "description": "Lorem ipsum dolor sit amet",
        "title": "Advise:",
      }
    },
    {
      "Advisory": {
        "Severe": "true",
        "description": "Lorem ipsum dolor sit amet",
        "title": "Advise:",
      }
    }
  ]
}

this is how my var $arrayItems is:

Array (
    [0] => Array (
        [Severe] => false
        [description] => Lorem ipsum dolor sit amet.
    [title] => Advise
    )
    [1] => Array (
        [Severe] => false
        [description] => Lorem ipsum dolor sit amet.
    [title] => Advise
    )
    [2] => Array (
        [Severe] => true
        [description] => Lorem ipsum dolor sit amet
    [title] => Advise
    )
)

but when i use the function json_encode like this: print json_encode(['Advisories' => array('Advisory'=>$arrayItems)]); the output is like this:

{
  "Advisories": {
    "Advisory": [
      {
        "Severe": "false",
        "description": "Lorem ipsum dolor sit amet",
        "title": "Advise",
      },
      {
        "Severe": "false",
        "description": "Lorem ipsum dolor sit amet",
        "title": "Advise",
      },
      {
        "Severe": "true",
        "description": "Lorem ipsum dolor sit amet",
        "title": "Advises ",
      }
    ]
  }
}

I dont know what else can i do to change the output,

Could you please help me? Regards

2
  • 1
    json_encode is just reflecting the structure that your data already has. Since you haven't shown us that data, or how you're generating it, there's really nothing more to say than to repeat your question back to you: you need to change the structure of your data from what it looks like, to what you want it to look like. Commented Jan 29, 2021 at 19:27
  • thank you for your answer i updated my cuestion and i added how my var is Commented Jan 29, 2021 at 20:03

1 Answer 1

1

To achieve that format you will have to modify the $arrayItems a little bit.

The below snippet will modify each subarray to include the 'Advisory' key which will be a property pointing to a json object (which holds the original subarray data).

$result = ['Advisories' => array_map(
    function ($subarray) {
        return ['Advisory' => $subarray];
    },
    $arrayItems
)];

return json_encode($result);

Result:

{
    "Advisories": [
        {
            "Advisory": {
                "Severe": false,
                "description": "Lorem ipsum dolor sit amet.",
                "title": "Advise"
            }
        },
        {
            "Advisory": {
                "Severe": false,
                "description": "Lorem ipsum dolor sit amet.",
                "title": "Advise"
            }
        },
        {
            "Advisory": {
                "Severe": true,
                "description": "Lorem ipsum dolor sit amet",
                "title": "Advise"
            }
        }
    ]
}
Sign up to request clarification or add additional context in comments.

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.