5

Is it possible to set property values of an object using a foreach loop?

I mean something equivalent to:

foreach($array as $key=>$value) {
    $array[$key] = get_new_value();
}

EDIT: My example code did nothing, as @YonatanNir and @gandra404 pointed out, so I changed it a little bit so it reflects what I meant

6
  • 2
    but you did nothing there.. you took the same keys and values and just assigned it to the same place there were before that... Commented Jul 30, 2015 at 12:04
  • When you use foreach it must be an array to loop through. Rather generate the array the convert it to object. Commented Jul 30, 2015 at 12:06
  • you mean doing $this->$key = $value maybe ? where $key is the name of the property you want to check ? Commented Jul 30, 2015 at 12:06
  • 1
    Are you looking for something like foreach (get_object_vars($obj) as $key => $value) { ... }? Commented Jul 30, 2015 at 12:07
  • @Phylogenesis looks to fit the question indeed... Commented Jul 30, 2015 at 12:30

5 Answers 5

6

You can loop on an array containing properties names and values to set.

For instance, an object which has properties "$var1", "$var2", and "$var3", you can set them this way :

$propertiesToSet = array("var1" => "test value 1", 
                         "var2" => "test value 2", 
                         "var3" => "test value 3");
$myObject = new MyClass();
foreach($propertiesToSet as $property => $value) {
    // same as $myObject->var1 = "test value 1";
    $myObject->$property = $value;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Would this example help at all?

$object = new stdClass;
$object->prop1 = 1;
$object->prop2 = 2;
foreach ($object as $prop=>$value) {
    $object->$prop = $object->$prop +1;
}
print_r($object);

This should output:

stdClass Object
(
    [prop1] => 2
    [prop2] => 3
)

Also, you can do

$object = new stdClass;
$object->prop1 = 1;
$object->prop2 = 2;
foreach ($object as $prop=>&$value) {
    $value = $value + 1;
}
print_r($object);

1 Comment

By default, objects are modifiable by reference. There is no need to prepend & before $value. You could just write ++$value; in the loop body and be done.
0

You can implement Iterator interface and loop through the array of objects:

foreach ($objects as $object) {
    $object->your_property = get_new_value();
}

Comments

0

Hacked away at this for a few hours and this is what i finally used. Note the parameters passed by reference in two places. One when you enter the method and the other in the foreach loop.

private function encryptIdsFromData(&$data){
    if($data == null)
        return;

    foreach($data as &$item){

        if(isset($item["id"]))
            $item["id"] = $this->encrypt($item["id"]);

        if(is_array($item))
            $this->encryptIdsFromData($item);
            
    }
}

Comments

0

I suggest the function for set all properties of object by properties from another object.

//from object "from" all values will be set into object $to
function setAllProperties(object $from, object $to)
{
  $map = (array) ($from);
  foreach ($map as $key => $value) {
    $position = mb_strrpos($key,  "\x00");
    $propertyName = substr($key, $position + 1);
    $setter = "set" . ucwords($propertyName);
    $to->$setter($value);
  }
  return $to;
}

We can call this function from the same namespace:

$object1= $this->setAllProperties($object2, $object1);

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.