5

I have the following result

Array ( 
      [0] => stdClass Object ( [name] => Identification ) 
      [1] => stdClass Object ( [name] => Assay ) 
      [2] => stdClass Object ( [name] => pH(Acidity/Alkalinity)) 
      [3] => stdClass Object ( [name] => Sterility )
    ) 

What i want is to separate the object array values using a comma and return as a string, so as to have this result:

 Identification, Assay, ph(Acid/Alkalinity), Sterility

I have tried the following

$data=(array)$result;
$answer=implode(",",$data);

This return :

Message: Object of class stdClass could not be converted to string

How best can this be achieved?

4

3 回答 3

33

您错过了处理对象数组的事实。

看起来你可以通过这样做来实现:

$output = array_map(function ($object) { return $object->name; }, $input);
echo implode(', ', $output);
于 2013-05-12T08:05:42.233 回答
1

要在“标题”属性中使用此功能,我做了:

echo implode('
', $output);
于 2016-10-14T13:26:01.623 回答
0

它将对象数组转换为字符串,您可以将其存储在数据库中,使用

serialize(array_of_object)

可能会帮助某人。我在我的情况下使用它。谢谢!

于 2020-10-03T04:43:30.307 回答