0

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.

4
  • Your use of in_array() should be throwing an error Commented Jan 29, 2020 at 21:12
  • @PatrickQ it does indeed. I did have some other code in place of in_array while testing a few different iterations, and had something without error in the if block, but it would still not add the new property. Commented Jan 29, 2020 at 21:25
  • Okay, and do you understand what the error ("in_array() expects parameter 2 to be array, string given") is telling you? Commented Jan 29, 2020 at 21:31
  • I do, I mistakenly am pointing to a string and not an array. Commented Jan 29, 2020 at 22:31

2 Answers 2

1

You can do it via transformation $user object to the array and back to object:

foreach ($users as $key=>&$user) {
    if (in_array("admin", $user->profile->type)) {

        // represents current $user->profile object as an array    
        $tmp = json_decode(json_encode($user->profile),true); 
        // add a new index
        $tmp['newproperty'] = "$key newItem"; 
        // transform back to an object
        $user->profile = (object)($tmp); 
    }
}

Demo

You can put it into the function like:

 function addNewProp($data, $prop_name, $prop_val){

    foreach ($data as $key=>$user) {
        if (in_array("admin", $user->profile->type)) {

            $tmp = json_decode(json_encode($user->profile),true); 
            $tmp[$prop_name] = $prop_val;
            $user->profile = (object)($tmp); 
        }
    }
    return $data;
 }  

Demo

Your input value of type has string datatype. Instead of

if (in_array("admin", $user->profile->type)) {

you should use

if ($user->profile->type === "admin") {
Sign up to request clarification or add additional context in comments.

Comments

0

Generally, I try and avoid doing things like (object)array(...) and json_decode(json_encode()) in PHP as it is not immediately clear to the reader why your doing this round about stuff, so I will provide an alternative answer.

 function addNewProp(array $users, $propName, $propVal)
 {
    for ($i = 0; $i < count($users); $i++) {
        // You may need to do extra checking here if profile and type are not required.
        if (isset($users[$i]->profile->type->admin)) {
            $users[$i]->profile->$propName = $propVal;
        }
    }
    return $users;
 }  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.