You can use array_map which map/return the new array as it was designed to this kind of job, supposed this :
// creating array of object
$a = (object) array('iditem' => 31702151,'idcolor' => 38 );
$b = (object) array('iditem' => 31702152,'idcolor' => 39 );
// placing above data to new array container
$ab = array($a,$b);
// callback function
function getData( $obj ) {
return $obj->idcolor;
}
// array_map the array using above callback
$colorid = array_map("getData", $ab );
print_r( $colorid );
Above code will produce Array( [0] => 39, [1] => 38 ). If you want it value directly stored in variable as a single string separate it by commas, just use the implode function to do so :
$newStr = implode(',', $colorid );
// which will produce 38,39
array_column, it now supports array of objects in PHP 7 - php.net/manual/en/function.array-column.phpforeach