Answer a question

\[a\] => Array (
  \[0\] => MongoId Object (
    \[$id\] => 506479dc9a5be1596b1bd97d
  ),
  \[1\] => MongoId Object (
    \[$id\] => 506479dc9a5be1596b1bd97d
  )
)

I have an array like this one. I need to change the values to string, to change it to something like this:

array (
  0 => "506479dc9a5be1596b1bd97d",
  1 => "506479dc9a5be1596b1bd97d",
)

This is my solution, but it is expensive and I will be using this in a for loop.

$yut = implode(",", $a);
$arr = explode(",", $yut);

Are there any other solution?

Answers

You can just use array_map to call MongoId::__toString() which would convert all Mongo Object in your array to string

$list = array_map(function($var){ return $var->__toString(); }, $yourArray);
Logo

MongoDB社区为您提供最前沿的新闻资讯和知识内容

更多推荐