sasori
sasori

Reputation: 5455

how to lowercase all the object properties(fieldnames) and remain as object and not array

yes, I have this object, and the properties represents the db table columns as the properties, but they are all in upper case...how to change all the properties of the object into lower case, without converting the object into array after shifting the properties from upper case to lower case ?.. i thought i can use array_change_key_case but this one returns array when i need the object and not array same with get_class_vars and get_object_vars

so how?

Upvotes: 2

Views: 3461

Answers (1)

Jon
Jon

Reputation: 437376

Your best option (and first choice) should be to intercept the object creation and make the properties lower case to begin with; that would be the cleanest and most performant.

If that is not possible for any reason and we are talking about a dumb object (of type stdClass) then the easiest is to go through an intermediate array and then into a new object:

$temp = (array)$old;
$new = (object)array_combine(array_map('strtolower', array_keys($temp)), $temp);

If the object is of another type then there is really no way to do what you suggest and you should step back and rethink the application from an earlier stage.

Upvotes: 5

Related Questions