Nawaz
Nawaz

Reputation: 303

PHP print array inside JSON

I need this type of json response in PHP

{
  "errors": [
    {
      "message": "Something went wrong!"
    }
  ]
}

Where "errors" is an array, and "message" is encoded in json

If I encode it something like this

json_encode (['errors' => ['message' => 'Unauthenticated.']])

it prints like

{
  "errors": {
    "message": "Unauthenticated."
  }
}

but I want it like above, any proper solution for this?

Upvotes: 2

Views: 106

Answers (1)

Gerjan
Gerjan

Reputation: 163

You could do it like this:

json_encode(['errors' => [['message' => 'Unauthenticated.']]]);

output:

{
    "errors": [
        {
            "message": "Unauthenticated."
        }
    ]
}

Upvotes: 1

Related Questions