Skip to content
Advertisement

Renaming an object key with a property of it’s own in PHP

I have an object like this in PHP-

JavaScript

I would like to take the country property and set it as the key of each values in the root object. A foreach resets the whole object value on each keys. Intended result is similar to the one right below –

JavaScript

Advertisement

Answer

As array_column can extract public property from objects, which are elements of arrays, you can:

JavaScript

But this method does not remove country property from each object.

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