Skip to content
Advertisement

PHP – How do I add a name to an array

I am building an array (json encoded) and returning it to the caller. My output currently looks like this:

JavaScript

I want to add a name so that output looks like this:

JavaScript

Here is my current code:

JavaScript

I am fairly new to OO coding, so I have not figured out how to initialize an object so that “orgs” is present. Maybe I don’t even need an object. Any help would be greatly appreciated.

Advertisement

Answer

Before you json encode the array, just wrap it in another one:

JavaScript

Then you json encode the new variable instead.

If you rather not create a new array, you can simply do:

JavaScript

This doesn’t really have anything to do with OOP since you’re just working with arrays. Associative arrays becomes objects when encoded into JSON.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement