Search code examples
phpcsvimplodearrays

Implode all the properties of a given name in an array of object - PHP


Is there a way to implode the values of similar objects contained in an array? I have an array of objects:

$this->inObjs

and I'd like a comma separated string of each of their messageID properties:

$this->inObjs[$i]->messageID

Is there an elegant way to do this or am I going to have to MacGyver a solution with get_object_vars or foreachs or something similar? Thanks for the help.


Solution

  • $messageIDArray;
    foreach($this->inObjs as $obj){
       $messageIDArray[] = $obj->messageID;
    }
    
    $string = implode(',',$messageIDArray);