How to use implode a column from an array of stdClass objects? How to use implode a column from an array of stdClass objects? arrays arrays

How to use implode a column from an array of stdClass objects?


You could use array_map() and implode()...

$a = array_map(function($obj) { return $obj->foo; },                array(1=>$obj1 , 2=>$obj2 , 3=>$obj3));$a = implode(", ", $a);


With PHP 7.0+ you can use array_column for this.

echo implode(',', array_column($a, 'foo'));


This is actually the best way I've found, it doesn't seem to be answered here properly as the array of objects should be able to handle dynamic size.

$str = implode(',', array_map(function($x) { return $x->foo; }, $a));