Looping through an Array of stdClass Objects, I'd like to add a new property to the Object. Of all the ways I've tried and researched, none end up adding the new property.
Array
(
[0] => stdClass Object
(
[id] => 12345678
[profile] => stdClass Object
(
[type] => superAdmin
)
)
[1] => stdClass Object
(
[id] => 89101112
[profile] => stdClass Object
(
[type] => admin
)
)
)
I have a function that takes in the above array, and then should iterate through it, if certain criteria met, add a new property to the object, and return the array.
public function addToProfile($usernameArr) {
$users = $usernameArr;
foreach ($users as $key => $user) {
if (in_array("admin", $user->profile->type)) {
$users[$key]->profile->newProperty = "newItem";
}
}
return $users;
}
I've also tried the json_decode(json_encode($users), true) way which did not end up with the result I'm looking for.
in_array()should be throwing an errorin_arraywhile testing a few different iterations, and had something without error in theifblock, but it would still not add the new property.