Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: array_map on object?

Tags:

oop

php

I'm trying to write a function that formats every (string) member/variable in an object, for example with a callback function. The variable names are unknown to me, so it must work with objects of all classes.

How can I achieve something similar to array_map or array_walk with objects?

like image 634
Christian Davén Avatar asked Feb 03 '23 13:02

Christian Davén


1 Answers

use get_object_vars() to get an associative array of the members, and use the functions you mentioned.

btw, you can also do a foreach on an object like you would on an array, which is sometimes useful as well.

like image 169
Sietse Avatar answered Feb 06 '23 11:02

Sietse