Skip to content
Advertisement

json encode specific object’s property

I have an array:

JavaScript

I want to return a json_encode for this array, but only for “key2” and “key3” attributes.

For the moment that:

JavaScript

But this is not okay as the array may also contain other properties. If it is possible, I prefer not to use loops…

(sorry for my english)

Advertisement

Answer

This solution uses array_map() and array_intersect_key():

JavaScript

…which prints:

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