Implode all the properties of a given name in an array of object - PHP [duplicate]
Asked Answered
R

6

5

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.

Rhotacism answered 6/4, 2011 at 5:18 Comment(0)
C
1
$messageIDArray;
foreach($this->inObjs as $obj){
   $messageIDArray[] = $obj->messageID;
}

$string = implode(',',$messageIDArray);
Concertina answered 6/4, 2011 at 5:26 Comment(3)
he said he knows about foreachBrimstone
I did not know what solution he did find, so I wrote mine. just a word foreach doesn't mean anything to me...Concertina
the first line should be $messageIDArray = [];Spat
D
4
$allMessageID = '';
foreach ($this->inObjs as $objectDetail) :
    $allMessageID[] = $objectDetail->messageID;
endforeach;

$allMessageID_implode = implode(",", $allMessageID);

echo $allMessageID_implode;
Davie answered 6/4, 2011 at 5:27 Comment(0)
S
3

If you can modify the class, you can implement __toString:

class MyObject {
    private $messageID = 'Hello';
    public function __toString() {
        return $this->messageID;
    }
}
// ...
$objectList = array(new MyObject, new MyObject);
echo implode(',', $objectList);
// Output: Hello,Hello
Sever answered 30/8, 2013 at 23:54 Comment(0)
C
2

The easiest way that I found is using array_map

$messageIDs = array_map( function($yourObject) { return $yourObject->messageID; }, $this->inObjs );
$string = implode(", ", $messageIDs );
Cleora answered 23/1, 2016 at 13:56 Comment(0)
C
1
$messageIDArray;
foreach($this->inObjs as $obj){
   $messageIDArray[] = $obj->messageID;
}

$string = implode(',',$messageIDArray);
Concertina answered 6/4, 2011 at 5:26 Comment(3)
he said he knows about foreachBrimstone
I did not know what solution he did find, so I wrote mine. just a word foreach doesn't mean anything to me...Concertina
the first line should be $messageIDArray = [];Spat
B
1

I usually make a Helper for this situation, and use it like this


function GetProperties(array $arrOfObjects, $objectName) {
     $arrProperties = array();
     foreach ($arrOfObjects as $obj) {
         if ($obj->$objectName) {
              $arrProperties[] = $obj->$objectName;
         }
     }
     return $arrProperties;
}

Brimstone answered 6/4, 2011 at 5:32 Comment(0)
K
1

Here is a two liner:

array_walk($result, create_function('&$v', '$v = $v->property;'));
$result = implode(',', $result);

Or:

array_walk($result, function(&$v, &$k) use (&$result) { $v = $v->name; } );
$result = implode(',', $result);

Where $v->property is your object property name to implode.

Also see array_map().

Kistler answered 4/10, 2013 at 14:8 Comment(1)
Why am I seeing , &$k and use (&$result) here? Please revisit this post. I want to use this page to close another.Sidelight

© 2022 - 2024 — McMap. All rights reserved.