How to get Database results as object instead of array?

This is a fine point (but will save me a lot of recoding) right now I call $arr->about_us instead of $arr[‘about_us’]

Is there a way to make it a standard object instead of an array?

json_decode(json_encode($array))

Are you sure you want to? Doing so in order to save a bit of coding doesn’t seem like the best reason to me.

PHP has a lot of native functions to work with arrays. And objects are by reference not copies.

True, you could save on the quotation marks and square brackets, but depending on what and how much work you’re doing with the results you might end up writing more code.

Ok sure - it’s a static array (or supposed to be) I can leave it as such as rework the rest.

Ps neat on the json_encode … I will have use for that :slight_smile:

Thanks again

How about:

(object)$array

Seems to be the same in my (limited) test.

should also work, though i’ve not tested it with complex things. Might work better, actually, since json_encode wont be able to transmit object types.

How are you currently fetching from the database?
You should be able to fetch data directly to an object.

1 Like

Probably but the code was to match the word index to the native language (ie make one column a key and another a value)

It would probably have been raise to somehow do that directly as an object … but now I am halfway through making it all arrays and don’t want to go back again

So it’s fine as an array - it works

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.